0%

RadioButton

RadioButton

RadioButton 是一个单选按钮,它继承自 CompoundButton,CompoundButton 继承自 Button,Button 继承自 TextView,TextView 继承自 View。

RadiaoButton 使用:

1
2
3
4
5
<RadioButton
android:id="@+id/radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />

RadiaoButton 在代码中使用:

1
2
RadioButton radioButton = findViewById(R.id.radio_button);
radioButton.setChecked(true);

RadioGroup

RadioGroup 使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:id="@+id/radio_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1" />

<RadioButton
android:id="@+id/radio_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2" />

<RadioButton
android:id="@+id/radio_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton3" />

</RadioGroup>

RadioGroup 在代码中使用:

1
2
3
4
5
6
7
8
RadioGroup radioGroup = findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = findViewById(checkedId);
Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});