簡體   English   中英

以線性布局從單選組中以編程方式添加單選按鈕值

[英]Getting programmatically added radio button value from radio group in linear layout

我試圖獲取添加到單選按鈕組並添加到線性布局的單選按鈕的值,然后從我的活動中調用該類。 這是我的代碼:

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();

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

您可能需要向單選按鈕添加一個id

您可以通過編程在單選按鈕上設置ID。 請在這里檢查

Android:以編程方式設置View.setID(int id)-如何避免ID沖突?

然后使用findviewbyId獲取單選按鈕

您可以嘗試以下操作:首先使用以下代碼將ID添加到廣播組: 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);

我通過在創建時向單選按鈕添加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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM