簡體   English   中英

如何使用SharedPreferences在Android中保存狀態?

[英]How to save states in Android using SharedPreferences?

如何在Android中保存復選框狀態。 條件:如果選中此復選框,則必須禁用單選按鈕,反之亦然。

 //More like an enable-disable button which should save 
  radioButtons states and then disable/enable them

 //RadioButtons states should be saved too
 //if radioeg1 was checked when checkbox was checked, its state should
     be saved     

 Eg.
  // cb = (Checkbox) findViewById(...) 
 //radioeg1 = (RadioButton) findViewById(...) 
 //radioeg2 = (RadioButton) findViewById(...)


if (cb.ischecked){

     //save cb state as checked

    //radioeg1.setEnabled(false) must be saved

   //radioeg2.setEnabled(false) must be saved

   //change the RadioButtons textcolor to grey? //Optional but an added Bonus if possible 
  //if not possible using this way, maybe change a textview's color to grey?
}

//在創建時檢查復選框的狀態

boolean checkBoxState = getSharedPreferences(context).getBoolean(“ checkbox_state”,false); //默認為未選中

if(checkBoxState){
    checkbox.setChecked(true);
    radioeg1.setEnabled(false);
    radioeg1.setEnabled(false);
}

//存儲更改后的復選框狀態checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

    @Override
    public void onCheckedChanged (CompoundButton buttonView,boolean isChecked){
    SharedPreferences.Editor editor = getSharedPreferences(context).edit();
    editor.apply();
    editor.putBoolean("checkbox_state", isChecked);
    if (isChecked) {
        radioeg1.setEnabled(false);
        radioeg1.setEnabled(false);
    }
}
});

你可以這樣做(假設this是一個類型的Context或子類Context )保存復選框狀態到SharedPreferences

SharedPreferences.Editor editor = PreferenceManager.getSharedPreferences (this).edit();

這基本上得到了一個SharedPreferences.Edit對象,該對象使您可以編輯共享首選項。 現在您可以在其中存儲內容了!

editor.putBoolean ("checkbox state", <Your boolean value here>);

現在您完成了!

嗯...雖然不完全...您仍然需要做一些我總是忘記的事情! - apply更改!

editor.apply();

現在你真的做。

要從共享首選項中獲取內容,請首先獲取一個共享首選項對象:(不是Editor !)

SharedPreferences prefs = PreferenceManager.getSharedPreferences (this);

然后,您只需調用getXXX方法:

boolean checkBoxState = prefs.getBoolean ("checkbox state", false);

第一個參數是密鑰。 您使用與用於保存它的密鑰相同的密鑰,即"checkbox state" 第二個參數是當找不到所需的鍵時將返回的內容。

編輯:

糟糕! 似乎我忘記了該方法的名稱。 應該是getDefaultSharedPreferences而不是getSharedPreferences 我的錯!

您基本上檢查布爾變量checkBoxState來查看是否禁用單選按鈕。

if (checkBoxState) {
    //If it is true, disable the radiobuttons
    <Your radio button name>.setEnabled (false);
}
SharedPreferences file;
private static int checkBox =1;
private static int radiobutton=0;
SharedPreferences.Editor editor;

In your OnCreate()
file = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor=file.edit();
 if(file.getInt("checkBox",0)==0)
    {


        //disable radiobutton here;


    }

    if(file.getInt("radiobutton",1)==1)
    {


        //disable checkBox here;


    }

//您也可以根據自己的需要將其他商品放置在自己想要的位置

Cb = (CheckBox)findViewById(R.id.CheckBox);
    Cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

            editor.putInt("checkBox", 0);


                editor.commit();
       }
   }
);  
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            editor.putInt("radiobutton", 1);


                editor.commit();
        }
    });

由此看來,如何在您的問題中實現Shared Preferences的總體思路可能很清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM