الحصول على قيمة زر الاختيار المضافة برمجيًا من مجموعة الراديو في التخطيط الخطي

StackOverflow https://stackoverflow.com//questions/22039914

سؤال

أحاول الحصول على قيمة زر الاختيار الذي أقوم بإضافته إلى مجموعة راديو والذي تتم إضافته إلى تخطيط خطي، ثم أقوم باستدعاء الفصل من نشاطي.هذا هو الكود الخاص بي:

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();
هل كانت مفيدة؟

المحلول 4

لقد قمت بحل المشكلة عن طريق إضافة مستمع onCheckChanged إلى أزرار الاختيار عند الإنشاء ثم حفظ القيمة المحددة في التفضيلات المشتركة.عندما كنت بحاجة إلى القيم، حصلت للتو على جميع التفضيلات المشتركة من خلال تكرار المعرفات التي استخدمتها كمفتاح في التفضيلات المشتركة.رمز بلدي:

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

طريقة التحميل المسبق:

private String loadSavedPreferences(String key) {

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

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

نصائح أخرى

قد يكون هذا مفيدًا حقًا:http://developer.android.com/guide/topics/ui/controls/radiobutton.html#HandlingEvents

قد تحتاج إلى إضافة id إلى أزرار الراديو الخاصة بك.

يمكنك تعيين معرف على زر الاختيار برمجياً.يرجى التحقق هنا

ذكري المظهر:View.setID(int id) برمجيًا - كيفية تجنب تعارض المعرفات؟

ثم استخدم findviewbyId للحصول على زر الاختيار

يمكنك تجربة هذا:قم أولاً بإضافة معرف إلى مجموعة الراديو باستخدام:الروبوت: معرف = "@ + معرف / مجموعة الراديو"

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

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

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

يمكنك أيضًا إضافة معرف برمجيًا باستخدام:

View.setId(int id);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top