单选按钮编程检查 [英] RadioButton check programatically

查看:112
本文介绍了单选按钮编程检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XML以下radigroup

 < RadioGroup中
     机器人:layout_width =WRAP_CONTENT
     机器人:layout_height =WRAP_CONTENT
     机器人:layout_gravity =CENTER_HORIZONTAL |底
     机器人:方向=横向
     机器人:ID =@ + ID / RadioGroup中>
  < / RadioGroup中>

和我检查的对象的数目在我的JSON和生成单选按钮和添加他们在RadioGroup中如下

 私人无效createRadioButton(INT nImages){
  最后的单选按钮[] = RB新单选[nImages]
  的for(int i = 0; I< nImages;我++){
    RB [I] =新的单选按钮(本);
    RB [I] .setId(ⅰ);
    radioGroup.addView(RB [I]);
  }
}

现在我需要知道如何了解和检查选定的单选按钮?

当我是很难codeD,以下是工作,

  radioGroup.check(R.id.radioButton0);

但现在我加入单选按钮编程,我不知道如何处理。

  radioGroup.check(??);


解决方案

如果你想在RadioGroup中一个特定的位置来选择单选按钮,可以使用

  radioGroup.check(radioGroup.getChildAt(位置).getId());

其中,位置在单选组的单选按钮的位置

I have the following radigroup in the xml

<RadioGroup
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center_horizontal|bottom"
     android:orientation="horizontal"
     android:id="@+id/radiogroup">
  </RadioGroup>

and I am checking the number of objects in my json and generate radiobuttons and add them in the radiogroup as follows.

private void createRadioButton(int nImages) {
  final RadioButton[] rb = new RadioButton[nImages];
  for(int i=0; i<nImages; i++){
    rb[i] = new RadioButton(this);
    rb[i].setId(i);
    radioGroup.addView(rb[i]);
  }
}

now I need to know how to know and check selected radio button ?

when I was hardcoded, the following was working,

 radioGroup.check(R.id.radioButton0);

but now I am adding radio buttons programmatically, I do not know how to handle

 radioGroup.check(??);

解决方案

If you want to select radio button at a particular position in radiogroup, you can use

radioGroup.check(radioGroup.getChildAt(position).getId());

where position is the position of the radio button in radio group

这篇关于单选按钮编程检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆