【问题标题】:Getting programmatically added radio button value from radio group in linear layout从线性布局的单选组中以编程方式添加单选按钮值
【发布时间】:2014-02-26 11:26:04
【问题描述】:

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

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 值的方法,它工作正常。

我获取控件并将其添加到包含视图的列表中,然后如果视图包含EditText,我会使用它来获取值:

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

【问题讨论】:

    标签: android android-radiogroup android-radiobutton


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以通过编程方式在单选按钮上设置一个 ID。请检查这里

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

      然后使用 findviewbyId 获取单选按钮

      【讨论】:

      • 我明白了,但我想首先从视图中获取单选组,然后是单选按钮,然后是值(如果可能的话)。它似乎适用于编辑文本。查看我的编辑
      【解决方案3】:

      你可以试试这个: 首先使用以下命令将 id 添加到 radiogroup: android: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);
      

      【讨论】:

      • 抱歉,开始时 xml 中不存在 RadioGroup。
      • 我已经编辑了答案,您可以在创建 RadioGroup 时以编程方式添加 ID
      • 嗨,我已经编辑了我的问题。我正在寻找与EditText 类似的方法
      • 如果可能的话。
      • 我认为不可能,因为单选按钮有各种子项,它不能自动获取所选单选按钮的文本。
      【解决方案4】:

      我通过在创建时向单选按钮添加一个 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多