Radion组内的单选按钮在Android中不会被选中

时间:2015-01-04 06:58:55

标签: android radio-button status radio-group

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

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>

2 个答案:

答案 0 :(得分:1)

这是你需要的完整答案,我试过这个,这是完美的。此代码将所选复选框的值存储在sharedpreferances中,并在需要时返回存储的值。

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

答案 1 :(得分:0)

您能描述实际流量吗?是否涉及用户选择单选按钮,屏幕关闭,然后用户返回到同一屏幕并且您想要显示所选的预先选择的单选按钮?

如果是这种情况,我相信您需要专门设置选中/选中的单选按钮,例如:     ((单选按钮)radioGroup.getChildAt(INDEX))setChecked(真);