单选按钮其实就是好几个按钮成为了一个大家族,我们每次只能够选择一个成员,创建单选按钮的方法就是把平常我们见到的普通按钮Button给他们装到一个盒子里面,让他们成为一个按钮组,所以就有了RadioGroup的产生。
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
RadioGroup>
以上代码就是我们创建了一个单选按钮组,在这个按钮组里面我们创建了两个按钮(当然我们也可以创建更多的),注意我们一定要把每一个Button给包含到RadioGroup中。
其中android:orientation="horizontal"
可以设置按钮组中按钮的排列方向
horizontal 按钮横向排列
vertical 按钮纵向排列
按钮组创建出来了,但是现在这些个代码还不能读取每个按钮的状态,接下来我们要做的就是获取每一个按钮的选中状态。我们有两种方式获得,第一种是当改变单选按钮组的值时获取,第二种是当单击其他按钮时获取。
1、改变单选按钮组的值时获取
首先我们要为刚才创建的按钮组设置一个id属性
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
RadioGroup>
然后在主方法中利用id获取按钮组对象,然后给他们添加专门用于单选按钮组的监听器
RadioGroup sex=(RadioGroup)findViewById(R.id.radioGroup1);
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group,int checkedId){
RadioButton r=(RadioButton)findViewById(checkedId);
Toast.makeText(MainActivity.this,"您选择的是"+r.getText(),Toast.LENGTH_LONG).show();
}
});
其中的r.getText()
就是得到这个按钮所对应的文本内容
2、单击其他按钮时获取
单击其他按钮时获取选中项的值时,首先需要在该按钮的单击事件监听器的onClick()方法中,通过for循环语句遍历当前单选按钮组,并根据被遍历到的单选按钮的isChecked()方法判断该按钮是否被选中,当被选中时,通过单选按钮的getText()方法获取对应的值,例如,要在单击“提交”按钮时,获取ID属性为radioGroup1的单选按钮组的值,代码如下
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
注意要给按钮组和组外的提交按钮设置id属性
主方法中的部分代码如下
public class MainActivity extends AppCompatActivity {
private RadioGroup sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sex=(RadioGroup)findViewById(R.id.radioGroup1);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
for(int i=0;i//根据索引值获取单选按钮
if(r.isChecked()){
Toast.makeText(MainActivity.this,"您选择的是"+r.getText(),Toast.LENGTH_LONG).show();
}
}
}
});
}
}
注意这里面我们先写了这句话private RadioGroup sex;
那是因为我们一会要在事件监听器的方法中使用sex,所以我们要把sex设置成全局的类型。
如果我们要想某些按钮在初始化的时候就已经默认被选中,那么我们可以这样写
"wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
其中的android:checked="true"
就代表按钮初始就已经被选中了
true 默认被选中
false 默认不被选中
当然默认被选中的按钮我们在程序运行的时候也可以把它进行取消,这在我们生活中都有这个体验。