Android radion 组内的单选按钮未保持选中状态

Radio Button inside radion group doesn't remains selected in Android

我在一个单选组中有三个单选按钮。通过从 xml 文件中删除 set selected 行,我基本上取消了所有单选按钮的选择。现在,当用户选择任何按钮时,它只工作一次,当它再次进入单选按钮选择时 window 它在选择状态之前已经丢失了所有。我尝试了很多陈述但没有成功。意味着我希望当用户选中任何按钮时,它将始终保持选中状态,直到用户不选中任何其他按钮。请帮我。下面是我的代码。

SelectedLanguage.java:

public class SelectLanguage extends ActionBarActivity {
    public static String lang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_language);

        RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
        RadioButton gujarati = (RadioButton) findViewById(R.id.radio0);
        RadioButton hindi = (RadioButton) findViewById(R.id.radio1);
        RadioButton english = (RadioButton) findViewById(R.id.radio2);

        //int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).getInt("my_selected_radio",0);
        //int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).edit().putInt("my_selected_radio",selectedValue).commit();

        group.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(checkedId == R.id.radio0) {
                    lang = "Gujarati";

                } else if(checkedId == R.id.radio1) {
                    lang = "Hindi";

                } else {
                    lang = "English";

                }

            }

        });

    }

select_language.xml:

<RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="25dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />
    </RadioGroup>

能否描述一下实际流程?是否涉及用户选择单选按钮,屏幕关闭,然后用户返回同一屏幕,您要将预先选择的单选按钮显示为选中?

如果是这样,我相信您需要专门设置 checked/selected 单选按钮,例如: ((RadioButton)radioGroup.getChildAt(INDEX)).setChecked(true);

这是满足您需求的完整答案,我已经尝试过了,效果很好。此代码将所选复选框的值存储在 sharedpreferences 中,并在需要时 returns 存储的值。

group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        pref = getApplicationContext().getSharedPreferences("MyPref", 0);
        Editor editor1 = pref.edit();
        editor1.remove("key_name");
        if (checkedId == R.id.radio0) {
            lang = "Gujarati";
            pref = getApplicationContext().getSharedPreferences("MyPref", 0);
            Editor editor = pref.edit();
            editor.putString("key_name", lang);
            editor.commit();
            // chk = pref.getString("key_name", null);
            txtV.setText(lang);

        } else if (checkedId == R.id.radio1) {
            lang = "Hindi";
            pref = getApplicationContext().getSharedPreferences("MyPref", 0);
            Editor editor = pref.edit();
            editor.putString("key_name", lang);
            editor.commit();
            // chk = pref.getString("key_name", null);
            txtV.setText(lang);
        } else if (checkedId == R.id.radio2) {
            lang = "English";
            pref = getApplicationContext().getSharedPreferences("MyPref", 0);
            Editor editor = pref.edit();
            editor.putString("key_name", lang);
            editor.commit();
            // chk = pref.getString("key_name", null);
            txtV.setText(lang);

        }
    }

});

pref = getApplicationContext().getSharedPreferences("MyPref", 0);
chk = pref.getString("key_name", null);
System.err.println("Get value is=====" + chk);
if (chk != null) {
    if (chk.equals("Gujarati")) {
        gujarati.setChecked(true);
    }
    if (chk.equals("Hindi")) {
        hindi.setChecked(true);
    }
    if (chk.equals("English")) {
        english.setChecked(true);
    }
}