zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    MT【90】图论基础知识及相关例题
    MT【89】三棱锥的体积公式
    Qt之界面实现技巧
    QCompleter自动补全
    Qt之开机自启动
    Qt之日志输出文件
    相遇Qt5
    QT中的pro文件的编写
    qt下的跨目录多工程编译
    用QT打开网页
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/11477901.html
Copyright © 2011-2022 走看看