2011-12-13 31 views
3

我有两个无线电组,每组有两个单选按钮。 对于每组中的第一个单选按钮,默认值为真(即,选中)。 当用户关闭任何单选按钮时,并且当用户从其他活动回来时,这些radiogroups/radiobuttons上所做的选择已经消失... 如何保存/恢复radiogroup/radiobutton选择状态? 这里是我的Java code..for一个RadioGroup中..如何在android中保存整个应用程序的redibutton状态?

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

请帮助。

+0

取一些变量并存储它的值。 –

+0

我在我的答案编辑示例链接.. – user370305

+0

看看我编辑的答案,我给一段代码.. – user370305

回答

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(); 
    } 
    }); 
    } 
+0

我对此非常陌生。你能给我一个代码吗? – shankey

+0

我需要时间来理解所有,请直接给我一个代码。 – shankey

+0

非常感谢您的先生,我会尝试使用该示例来做到这一点。 – shankey

3

你需要重写的onSaveInstanceState(捆绑savedInstanceState)和写要更改的捆绑参数这样的应用程序的状态值:

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

的包基本上是一种存储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"); 
} 

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

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 
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可以帮你做任何你需要的。

0

尝试使用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检查单选按钮。

相关问题