[Android Development] Common 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

  • Radio control
  • Can be used with RadioGroup, only one can be selected
  • Difference from CheckedBox:
    • Can't become unselected by clicking
    • A group of RadioButton, only one can be selected
    • It is displayed in a circle by default in most UI frameworks
<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

  • Switch status in the program
  • Two states:
    • 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"/>

Guess you like

Origin blog.csdn.net/weixin_42020386/article/details/112790569