0

我正在处理自定义单选按钮。它工作正常(因为它在 options.xml 中定义)但是当我从 options.xml 切换到 main.xml 时,它变成默认值,意味着它不再突出显示。它应该像我按下它之前一样工作它不应该变成默认值..这是radiobutton_selector.xml

        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/radio_down" android:state_checked="true"/>
        <item android:drawable="@drawable/radio" android:state_checked="false"/>
        </selector>

我在 options.xml 中使用这些来调用单选按钮设置。

        <RadioGroup
            android:id="@+id/sound"
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:layout_gravity="right"
            android:layout_marginRight="0dp"
            android:layout_marginTop="50dp"
            android:orientation="vertical"
            android:padding="0dp" >

            <RadioButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:button="@drawable/radiobutton_selector"
                android:id="@+id/on"
                />

            <RadioButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:button="@drawable/radiobutton_selector"
                android:id="@+id/off"
                />
        </RadioGroup>

请帮我找出问题所在。提前致谢 !!!!!

4

1 回答 1

2

你可以为你的RadioGroup. 您需要保存您选择的单选按钮,为此您可以使用如下所示的一个变量。

int check_radio=-1;
public static final int RADIO_BUTTON_ON=1;
public static final int RADIO_BUTTON_OFF=2;

       mRadioGroup
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group,
                            int checkedId) {

                        switch(checkedId){

                                            case R.id.on:
                                            //Radio Button on is True
                                            check_radio=RADIO_BUTTON_ON;
                     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                     SharedPreferences.Editor editor = settings.edit();
                      editor.putInt("RadioButton", RADIO_BUTTON_ON);
                     // Commit the edits!
                     editor.commit();
                                            break;
                                           case R.id.off:
                                          //Radio Button off is True
                                           check_radio=RADIO_BUTTON_OFF;
                       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                     SharedPreferences.Editor editor = settings.edit();
                      editor.putInt("RadioButton", RADIO_BUTTON_OFF);
                     // Commit the edits!
                     editor.commit();
                                          break;
                    }
                });

现在你的 Activity 的 Resume 你可以检查一个条件,如下所示。从 SharedPrefrence 中获取价值,如下面的代码;

   //If you have save your value in SharedPrefrence it will return your stored int value.
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   check_radio = settings.getInt("RadioButton", -1);


  if(check_radio==RADIO_BUTTON_ON){
     mRadioOn.setChecked(true);
  }else if(check_radio==RADIO_BUTTON_OFF){
    mRadioOff.setChecked(true);
  }

您可以通过以下步骤使用 SharedPreferences

   public static final String PREFS_NAME = "MyPrefsFile";
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
于 2012-04-14T08:34:05.843 回答