如何检索用户选择的动态创建的单选按钮的文本?这是我的代码:
RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new
RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < 4; i++){
final RadioButton newRadioButton = new RadioButton(this);
c3 = db.getAns(3);
for (int j=0;j<i;j++)
c3.moveToNext();
label = c3.getString(0);
newRadioButton.setText(label);
newRadioButton.setId(6);
radiogroup.addView(newRadioButton, layoutParams);
Run Code Online (Sandbox Code Playgroud)
等待回复,Maqsood
Rob*_*ond 17
惊讶的是,没有一种更简单的方法.如果您打算做一些特殊的事情,但根据哪个按钮,你应该检查ID而不是Label.
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
void onCheckedChanged(RadioGroup rg, int checkedId) {
for(int i=0; i<rg.getChildCount(); i++) {
RadioButton btn = (RadioButton) rg.getChildAt(i);
if(btn.getId() == checkedId) {
String text = btn.getText();
// do something with text
return;
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
我认为有一种更简单的方法可以做到这一点......
我刚刚创建了一个单选按钮,其中的按钮ID已经过检查,工作正常.
解决方案看起来像这样
RadioButton TheTextIsHere = (RadioButton) findViewById(RadioGroup.getCheckedRadioButtonId());
Run Code Online (Sandbox Code Playgroud)
所以现在你有了一个RadioButton,它引用了放入RadioGroup的RadioButton然后你就可以轻松地......
TheTextIsHere.getText().toString();
Run Code Online (Sandbox Code Playgroud)
希望我帮助:)
| 归档时间: |
|
| 查看次数: |
18254 次 |
| 最近记录: |