1. CheckBox compound box component
CheckBox inherits from the CompoundButton class, and implements the functions and characteristics of the compound box by inheriting and extending the properties and methods of the CompoundButton class. , which provides a button with two states: selected and unselected, which changes when the button is pressed.
Some common properties and methods of the CompoundButton class include:
- isChecked(): Returns the current selected status of the compound button, and the return value is of Boolean type.
- setChecked(boolean checked): Set the checked state of the compound button, the parameter is of Boolean type.
- setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener): Set the selected state change listener of the compound button. When the selected state of the compound button changes, the callback method in the listener will be triggered.
- setButtonDrawable(Drawable drawable): Set the icon of the composite button, the parameter is Drawable type.
- setButtonTintList(ColorStateList tint): Set the icon color of the composite button, the parameter is ColorStateList type.
Instructions:
First we add a CheckBox component to the layout file:
<CheckBox
android:id="@+id/taskCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="任务名称" />
Set the checkbox listener in code:
CheckBox taskCheckbox = findViewById(R.id.taskCheckbox);
taskCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 处理复选框选中状态改变的逻辑
if (isChecked) {
// 复选框被选中
// 执行相应的操作
} else {
// 复选框被取消选中
// 执行相应的操作
}
}
});
Determine whether the click event occurs by monitoring the isChecked value. Through the above code, you can implement click monitoring through CheckBox.
2. Switch components
The Switch component is a commonly used component in Android development, used to switch the switch state. It's a draggable slider that toggles between on and off.
Give a simple example:
First we add the Switch control in the layout file
<Switch
android:id="@+id/switchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开关"
android:checked="false" />
Insert the following code in the MainActivity class to call the component to implement monitoring:
public class MainActivity extends AppCompatActivity {
private Switch switchButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchButton = findViewById(R.id.switchButton);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// 当Switch组件状态为打开时执行的代码
Toast.makeText(MainActivity.this, "开关已打开", Toast.LENGTH_SHORT).show();
} else {
// 当Switch组件状态为关闭时执行的代码
Toast.makeText(MainActivity.this, "开关已关闭", Toast.LENGTH_SHORT).show();
}
}
});
}
}