2011-12-13 53 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檢查單選按鈕。

相關問題