我正在尝试获取添加到单选组并添加到线性布局的单选按钮的值,然后从我的活动中调用该类。这是我的代码:
MultiChoice.java
public class MultipleChoice {
Context context;
List<String> choice_values;
String hint;
public MultipleChoice (Context context, String hint, List<String> choice_value_array){
choice_values = new ArrayList<String>();
this.context = context;
this.choice_values = choice_value_array;
this.hint = hint;
}
public View createRadioGroup(){
LinearLayout llContainer = new LinearLayout(context);
llContainer.setOrientation(LinearLayout.VERTICAL);
llContainer.addView(hintTextView());
llContainer.addView(radioButtons());
return llContainer;
}
private TextView hintTextView() {
// TODO Auto-generated method stub
TextView tvHint = new TextView(context);
tvHint.setText(hint);
return tvHint;
}
private RadioGroup radioButtons() {
// TODO Auto-generated method stub
RadioGroup rbGroup = new RadioGroup(context);
rbGroup.setOrientation(RadioGroup.VERTICAL);
for (String value : choice_values){
RadioButton rbValue = new RadioButton(context);
rbGroup.addView(rbValue);
rbValue.setText(value);
}
return rbGroup;
}
}
这就是我在我的活动中创建控件的方式:
LinearLayout template_container = (LinearLayout) findViewById(R.id.llTemplate);
MultipleChoice mcControl = new MultipleChoice(getApplicationContext(), parts[0], choices);
control = mcControl.createRadioGroup();
template_container.addView(control);
我已经尝试过这样的事情,但我不确定我是否正在尝试正确的方法,因为它不起作用:
View child = template_container.getChildAt(i);
LinearLayout v = ((LinearLayout)child);
View rgView = v.getChildAt(1);
RadioGroup rg = ((RadioGroup)rgView);
RadioGroup
添加并显示正常。我要做的就是获取所选单选按钮的值。提前致谢!
编辑
这就是我获得 an 值的方式EditText
,它工作正常。
我得到控件并将其添加到包含视图的列表中,然后如果视图包含以下内容,我会使用它来获取值EditText
:
String text = ((EditText)view).getText().toString().trim();