如何在android中的整个应用程序中保存单选按钮的状态?

时间:2011-12-13 09:31:49

标签: android radio-button

我有两个无线电组,每组有两个无线电按钮。 对于每组中的第一个无线电按钮,默认值为真(即检查)。 当用户在任何单选按钮上进行任务时,当用户从其他活动中恢复时,对这些无线电组/无线电按钮的选择就会消失...... 如何保存/恢复radiogroup / radiobutton选择状态? 这是我的java代码..对于一个radiogroup ..

        final RadioButton radioOn = ( RadioButton )findViewById(
        R.id.rbOn );
    final RadioButton radioOff = ( RadioButton )findViewById(
        R.id.rbOff );
    radioOn.setChecked( true );
    radioOn.setOnClickListener( auto_lock_on_listener );
    radioOff.setOnClickListener( auto_lock_off_listener );

请帮忙。

5 个答案:

答案 0 :(得分:2)

您需要覆盖onSaveInstanceState(Bundle savedInstanceState)并将要更改的应用程序状态值写入Bundle参数,如下所示:

@Override

    public void onSaveInstanceState(Bundle savedInstanceState) {
      // Save UI state changes to the savedInstanceState.
      // This bundle will be passed to onCreate if the process is
      // killed and restarted.
      savedInstanceState.putBoolean("MyBoolean", true);
      savedInstanceState.putDouble("myDouble", 1.9);
      savedInstanceState.putInt("MyInt", 1);
      savedInstanceState.putString("MyString", "Welcome back to Android");
      // etc.
      super.onSaveInstanceState(savedInstanceState);
    }

Bundle本质上是一种存储NVP(“名称 - 值对”)映射的方式,它将被传递到onCreate和onRestoreInstanceState,你可以在这里提取这样的值:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
  double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("MyInt");
  String myString = savedInstanceState.getString("MyString");
}

您通常使用此技术存储应用程序的实例值(选择,未保存的文本等)。

答案 1 :(得分:1)

尝试使用SharedPreferences

保存您的无线电组状态
     RadioGroup rG1 = (RadioGroup)findViewById(R.id.radioGroup1);
        int rG1_CheckId = rG1.getCheckedRadioButtonId();

SharedPreferences rG1Prefs = getSharedPreferences("rG1Prefs", MODE_WORLD_READABLE);
                SharedPreferences.Editor prefsEditor = rG1Prefs.edit();
                prefsEditor.putInt("rG1_CheckId", rG1_CheckId);
                prefsEditor.commit();

并将此行用于取回已选中的单选按钮ID。

SharedPreferences rG1Prefs = this.getSharedPreferences("rG1Prefs", MODE_WORLD_READABLE);
        rG1Prefs.getInt("rG1_CheckId", null);

并使用此ID检查单选按钮。

答案 2 :(得分:0)

使用共享首选项,保存单选按钮的状态,或者您可以使用应用程序变量(在该活动中声明为全局静态的变量,您可以在任何活动)

但我认为共享偏好很好..

查看Android - Shared Preferences

请看这个例子How to save login info to Shared Preferences Android(remember details feature)

修改

public  class Test extends Activity   {     

private SharedPreferences prefs;
private String prefName = "MyPref";
private SharedPreferences.Editor editor; 
private static final String CHECK_STATE = "checkBox_State";
private boolean check_state;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    //---getting the SharedPreferences object....
    prefs = getSharedPreferences(prefName, MODE_PRIVATE);    
    editor = prefs.edit();

    radioOn.setOnClickListener(new OnClickListener()  {
        public void onClick(View v)  {               
            if (((CheckBox) v).isChecked()) {
                check_state = true;                                 
            } else {
                check_state = false;                                    
            }
        }
    });

            // storing in shared preferences...
    editor.putBoolean(CHECK_STATE, check_state );             
    editor.commit();
    }
   });
   }

答案 3 :(得分:0)

您可以存储到SharedPreference

简单的例子

SharedPreferences settings = getSharedPreferences("on_off", 0);    
boolean silent = settings.getBoolean("onoff", false);
////// to set the value use editor object from the SharedPreferences

Editor editor = settings.edit();
editor.putBoolean("onoff", true);
editor.commit(); // to save the value into the SharedPreference

答案 4 :(得分:0)

使用此代码 -

mFillingGroup.check(whichFilling);
    mAddMayoCheckbox.setChecked(addMayo);
    mAddTomatoCheckbox.setChecked(addTomato);

    /**
     * We also want to record the new state when the user makes changes,
     * so install simple observers that do this
     */
    mFillingGroup.setOnCheckedChangeListener(
            new RadioGroup.OnCheckedChangeListener() {
                public void onCheckedChanged(RadioGroup group,
                        int checkedId) {
                    // As with the checkbox listeners, rewrite the
                    // entire state file
                    Log.v(TAG, "New radio item selected: " + checkedId);
                    recordNewUIState();
                }
            });

    CompoundButton.OnCheckedChangeListener checkListener
            = new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // Whichever one is altered, we rewrite the entire UI state
            Log.v(TAG, "Checkbox toggled: " + buttonView);
            recordNewUIState();
        }
    };
    mAddMayoCheckbox.setOnCheckedChangeListener(checkListener);
    mAddTomatoCheckbox.setOnCheckedChangeListener(checkListener);
}

/**
 * Handy helper routine to write the UI data to a file.
 */
void writeDataToFileLocked(RandomAccessFile file,
        boolean addMayo, boolean addTomato, int whichFilling)
    throws IOException {
        file.setLength(0L);
        file.writeInt(whichFilling);
        file.writeBoolean(addMayo);
        file.writeBoolean(addTomato);
        Log.v(TAG, "NEW STATE: mayo=" + addMayo
                + " tomato=" + addTomato
                + " filling=" + whichFilling);
}

并且,this可以帮助您做任何您需要的事情。