RadioButton选中值的获取
首先RadioButton是嵌套在RadioGroup中的,即一个RadioGoup中可以拥有多个RadioButton,但是一般至少是两个。而在布局文件中,RadioButton外部嵌套RadioGroup,因此在获取RadioButton的选中值时应该先获取当前RadioGroup,利用RadioGroup得到用户已经选中的RadioButton,这样就可以利用getText()方法获取选中内容。
第一种方式:
通过radioGroup.getCheckedRadioButtonId()来得到选中的RadioButton的ID,从而利用findviewbyid得到RadioButton进而获取选中值
1、布局文件
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="wf.com.radiobuttondemo.MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别是:"
/>
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
android:id="@+id/radioMan"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="男"
/>
android:id="@+id/radioWonan"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="女"
/>
android:id="@+id/txt_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别是:"
/>
2、MainActivity
public class MainActivity extends Activity {
@BindView(R.id.radioMan)
RadioButton radioMan;
@BindView(R.id.radioWonan)
RadioButton radioWonan;
@BindView(R.id.radioGroup)
RadioGroup radioGroup;
@BindView(R.id.txt_sex)
TextView txtSex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);//通过RadioGroup的setOnCheckedChangeListener()来监听选中哪一个单选按钮
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
selectRadioButton();
}
});
}
private void selectRadioButton() {//通过radioGroup.getCheckedRadioButtonId()来得到选中的RadioButton的ID,从而得到RadioButton进而获取选中值
RadioButton rb = (RadioButton)MainActivity.this.findViewById(radioGroup.getCheckedRadioButtonId());
txtSex.setText(rb.getText());
}
}
第二种方式:
需要利用一下三个方法
(1)radiogroup.getChildCount() 获取radiogroup中子组件(radioButton)的数目
(2)radiogroup.getChildAt() 根据索引获取当前索引对应的radioButton
(3)radiobutton.isChecked() 判断当前组件是否被选中
整体思路是,对radiogroup中组件进行循环,依次判断isChecked(),从而找到选中的组件
1、布局文件
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="wf.com.radiobuttondemo.MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别是:"
/>
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
android:id="@+id/radioMan"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="男"
/>
android:id="@+id/radioWonan"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="女"
/>
android:id="@+id/txt_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别是:"
/>
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
/>
2、MainActivity
public class MainActivity extends Activity {
@BindView(R.id.radioMan)
RadioButton radioMan;
@BindView(R.id.radioWonan)
RadioButton radioWonan;
@BindView(R.id.radioGroup)
RadioGroup radioGroup;
@BindView(R.id.txt_sex)
TextView txtSex;
@BindView(R.id.btn_submit)
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btn_submit)
public void onClick() {
for(int i = 0 ;i < radioGroup.getChildCount();i++){
RadioButton rb = (RadioButton)radioGroup.getChildAt(i);
if(rb.isChecked()){
txtSex.setText(rb.getText());
break;
}
}
}
}