一、应用场景:类似单选题的场景
二、效果图:
三、代码:
main.xml中添加Button控件:
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioGroupDemo"/>
activity_main.xml中拖入4个radiobutton控件:
效果图:
.xml中的配置如下:
<TextView
android:id="@+id/radiohello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radiohello" />
<RadioGroup
android:id="@+id/radiogroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="3px"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="46dp"
android:text="@string/music" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_below="@+id/radioButton1"
android:layout_marginTop="21dp"
android:text="@string/gym" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton2"
android:layout_below="@+id/radioButton2"
android:layout_marginTop="23dp"
android:text="@string/dance" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton3"
android:layout_below="@+id/radioButton3"
android:layout_marginTop="25dp"
android:text="@string/lookBook" />
</RadioGroup>
string.xml中为每个按钮命名:
<string name="radiohello">选择你最喜欢的课程:</string>
<string name="music">音乐</string>
<string name="gym">体育</string>
<string name="dance">舞蹈</string>
<string name="lookBook">看书</string>
MainActivity.java主函数的内容:
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
this.setContentView(R.layout.activity_main); //读取控件
textView = (TextView) findViewById(R.id.radiohello);
radiogroup = (RadioGroup)findViewById(R.id.radiogroup1);
radio1 = (RadioButton) findViewById(R.id.radioButton1);
radio2 = (RadioButton) findViewById(R.id.radioButton2);
radio3 = (RadioButton) findViewById(R.id.radioButton3);
radio4 = (RadioButton) findViewById(R.id.radioButton4);
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String text="我最喜欢运动是";
if (checkedId == radio1.getId()) {
text+=radio1.getText().toString();
textView.setText(text);
} else if(checkedId == radio2.getId()){
text+=radio2.getText().toString();
textView.setText(text);
}else if(checkedId == radio3.getId()) {
text += radio3.getText().toString();
textView.setText(text);
}else if(checkedId == radio4.getId()) {
text += radio4.getText().toString();
textView.setText(text);
}
}
});
}
四、技术总结
1,RadioGroup
为单项选择按钮组,其可以包含多个单选按钮即 RadioButton,为用户提供多选一的选择方式。
2,一个RadioGroup 包含多个 RadioButton的情况下,多个 RadioButton 之间自动形成互斥关系,仅有一个可以被选择。
3,事件监听接口使用的是
RadioGroup.OnCheckedChangeListener()方法,使用
setOnCheckedChangeListener() 方法将监听器设置到单选按钮上。