以线性布局从无线电组中以编程方式添加单选按钮值

时间:2014-02-26 11:26:05

标签: android android-radiogroup android-radiobutton

我正在尝试获取我添加到广播组的单选按钮的值,并将其添加到线性布局,然后从我的活动中调用该类。这是我的代码:

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并显示正常。我想要做的就是获取所选单选按钮的值。提前谢谢!

修改

这就是我获取EditText的值的方法,它可以正常工作。

我获取控件并将其添加到包含Views的List中,然后我用它来执行此操作以在视图包含EditText时找到值:

String text = ((EditText)view).getText().toString().trim();

4 个答案:

答案 0 :(得分:0)

这可能真的很有帮助: http://developer.android.com/guide/topics/ui/controls/radiobutton.html#HandlingEvents

您可能需要在单选按钮上添加id

答案 1 :(得分:0)

您可以以编程方式在单选按钮上设置ID。请在这里查看

Android: View.setID(int id) programmatically - how to avoid ID conflicts?

然后使用findviewbyId获取单选按钮

答案 2 :(得分:0)

你可以试试这个: 首先使用以下方法向radiogroup添加id: 的机器人:ID = “@ + ID / RadioGroup中”

RadioGroup rbGroup = (RadioGroup)findViewById(R.id.radiogroup);

int RadioButtonId=rbGroup.getCheckedRadioButtonId();
        View radioButton = rbGroup.findViewById(RadioButtonId);

        String = Integer.toString(rbGroup.indexOfChild(radioButton)+1);

您还可以使用以下方式以编程方式添加ID:

View.setId(int id);

答案 3 :(得分:0)

我通过在创建时向单选按钮添加onCheckChanged侦听器来解决问题,然后将所选值保存到共享首选项。当我需要这些值时,我通过迭代我用作共享首选项中的键的ID来获得所有共享首选项。我的代码:

private RadioGroup radioButtons(final int radio_id) {
    // TODO Auto-generated method stub
    RadioGroup rbGroup = new RadioGroup(context);
    rbGroup.setOrientation(RadioGroup.VERTICAL);
    for (final String value : choice_values) {
        RadioButton rbValue = new RadioButton(context);
        rbGroup.addView(rbValue);
        rbValue.setText(value);
        rbValue.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                savePreferences("" + radio_id, value);
            }

        });
    }
    return rbGroup;
}



private void savePreferences(String key, String value) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

获得价值观:

int radio_check = 0;
for (View view : addedControls) {
            String entered_value = getControlValue(view, radio_check);
            radio_check++;
        }

在我的getValue()方法中:

text = loadSavedPreferences("" + (radio_check));

LoadPrefs方法:

private String loadSavedPreferences(String key) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    String name = sharedPreferences.getString(key, "Default");
    return name;
}