ToggleButton 和 Switch

      

 

 

界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <!-- 定义一个ToggleButton按钮 -->
    <ToggleButton android:id="@+id/toggle"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textOff="横向排列"
                  android:textOn="纵向排列"
                  android:checked="true"/>
    <Switch android:id="@+id/switcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="横向排列"
            android:textOn="纵向排列"
            android:thumb="@drawable/check"
            android:checked="true"/>
    <!-- 定义一个可以动态改变方向的线性布局 -->
    <LinearLayout android:id="@+id/test"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
        <!-- 下面省略了三个按钮的定义 -->
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="测试按钮一"
        />
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="测试按钮二"
        />
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="测试按钮三"
        />
    </LinearLayout>
</LinearLayout>
View Code

我们这里的ToggleButton和Switch的作用都是改变下面三个按钮的排列方式,所以我们给他们都绑定同一个Listerner

android.widget.CompoundButton.OnCheckedChangeListener

 代码如下:

package com.example.tooglebutton

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.RadioGroup
import android.widget.Switch
import android.widget.ToggleButton
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.CompoundButton.OnCheckedChangeListener
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val toggleButton = findViewById<ToggleButton>(R.id.toggle)
        val switchButton = findViewById<Switch>(R.id.switcher)
        val test = findViewById<LinearLayout>(R.id.test)
        val listener = OnCheckedChangeListener{button,isChecked ->
            if (isChecked)
            {
                test.orientation=LinearLayout.VERTICAL
                toggleButton.isChecked=true
                switchButton.isChecked=true
            }
            else
            {
                test.orientation=LinearLayout.HORIZONTAL
                toggleButton.isChecked=false
                switchButton.isChecked=false
            }
        }
        toggleButton.setOnCheckedChangeListener(listener)
        switchButton.setOnCheckedChangeListener(listener)
    }
}
View Code

 

posted @ 2019-09-06 19:57  超级学渣渣  阅读(490)  评论(0编辑  收藏  举报