Android Studio如何获取radiobutton选中的值
(1)radiogroup.getChildCount() 获取radiogroup中子组件(radioButton)的数目
(2)radiogroup.getChildAt() 根据索引获取当前索引对应的radioButton
(3)radiobutton.isChecked() 判断当前组件是否被选中
整体思路是,对radiogroup中组件进行循环,依次判断isChecked(),从而找到选中的组件
@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;
}
}
}
原文链接:https://blog.csdn.net/ckq5254/article/details/54929296