【Android开发】常用Buttons

CheckedBox

 	<CheckBox
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="CheckBox" />
override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_buttons)

        val checkBox = findViewById<CheckBox>(R.id.checkbox)
        //设置容器是否选中
        checkBox.isChecked = false
        //获取选中的状态
        val isChecked = checkBox.isChecked
		//监听选中的状态
        checkBox.setOnCheckedChangeListener {
    
     buttonView, isChecked ->
            Log.e(
                "Tag",
                "onCheckedChanged$isChecked"
            )
        }
    }

RadioButton

  • 单选控件
  • 可以和RadioGroup一起使用,只能选择一个
  • 和CheckedBox区别:
    • 通过点击无法变为未选中
    • 一组RadioButton,只能选中一个
    • 在大部分UI框架中默认以圆形显示
<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButtion1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="RadioButton"/>
        <RadioButton
            android:id="@+id/radioButtion2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="RadioButton"/>
        <RadioButton
            android:id="@+id/radioButtion3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="RadioButton"/>

    </RadioGroup>

ToggleButton

  • 切换程序中的状态
  • 两种状态:
    • android:textOn=“hello”
    • android:textOff=“byebye”
    • setChecked(boolean)
  • setOnCheckedChangeListener
    <ToggleButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toggleButton"
        android:textOn="hello"
        android:textOff="byebye"
        android:checked="true"
        android:text="ToggleButton"/>

猜你喜欢

转载自blog.csdn.net/weixin_42020386/article/details/112790569