单选钮(RadioButton)、复选框(CheckBox)、状态开关按钮(ToggleButton)和开关(Switch)是用户界面中最普通的UI组件,它们都继承了Button类,因此都可直接使用Button支持的各种属性和方法。
RadioButton,CheckBox与普通按钮不同的是,它们多了一个可选中的功能,因此RadioButton,CheckBox都可额外指定一个android:checked属性,该属性用于指定RadioButton、CheckBox初始时是否被选中。
RadioButton与 CheckBox的不同之处在于,一组RadioButton只能选中其中一个,因此RadioButton通常要与RadioGroup一起使用,用于定义一组单选钮。
实例——利用单选按钮、复选框获取用户信息

定义布局
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp">
<TableRow android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:" />
<RadioGroup
android:id="@+id/rg"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
RadioGroup>
TableRow>
<TableRow android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的颜色:" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="红色" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色" />
LinearLayout>
TableRow>
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
TableLayout>

逻辑代码
package com.dingjiaxiong.checkbuttontest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取界面上的组件
RadioGroup rg = findViewById(R.id.rg);
TextView show = findViewById(R.id.show);
//为RadioGroup组件的点击事件绑定
rg.setOnCheckedChangeListener((group, checkedId) -> {
String tip = checkedId == R.id.male ? "您的性别是男" : "您的性别是女";
show.setText(tip);
});
}
}
运行效果

状态开关按钮(ToggleButton)和开关(Switch)也是由Button派生出来的,因此它们的本质也是按钮,Button支持的各种属性、方法也适用于ToggleButton和 Switch。从功能上来看,ToggleButton .Switch与CheckBox复选框非常相似,它们都可以提供两种状态。不过 ToggleButton、Switch与CheckBox的区别主要体现在功能上,ToggleButton、Switch通常用于切换程序中的某种状态。
ToggleButton支持的XML属性及相关方法:

Switch支持的XML属性及方法:


实例——动态控制布局
创建一个新模块

布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="横向排列"
android:textOn="纵向排列" />
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="横向排列"
android:textOn="纵向排列"
android:thumb="@drawable/check" />
<LinearLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3" />
LinearLayout>
LinearLayout>
逻辑代码
package com.dingjiaxiong.togglebuttontest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = findViewById(R.id.toggle);
Switch switcher = findViewById(R.id.switcher);
LinearLayout test = findViewById(R.id.test);
CompoundButton.OnCheckedChangeListener listener = (button , ischecked) -> {
if (ischecked){
//设置linearlayout垂直布局
test.setOrientation(LinearLayout.VERTICAL);
toggle.setChecked(true);
switcher.setChecked(true);
}
else{
//设置水平布局
test.setOrientation(LinearLayout.HORIZONTAL);
toggle.setChecked(false);
switcher.setChecked(false);
}
};
toggle.setOnCheckedChangeListener(listener);
switcher.setOnCheckedChangeListener(listener);
}
}
运行效果
