[英]How to save the progress of a ProgressBar using SharedPreferences in Android Studio - Java?
[英]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.