Java 如何使用SharedReferences在Android中保存状态?
如何在Android中保存复选框状态。 条件:如果选中复选框,则必须禁用单选按钮,反之亦然Java 如何使用SharedReferences在Android中保存状态?,java,android,sharedpreferences,state,Java,Android,Sharedpreferences,State,如何在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
//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=getSharedReferences(context).getBoolean(“checkbox\u state”,false)//默认未选中
if(checkBoxState){
checkbox.setChecked(true);
radioeg1.setEnabled(false);
radioeg1.setEnabled(false);
}
//更改时存储复选框状态
setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
您可以这样做(假设this
属于Context
类型或Context
的子类)以将复选框状态保存到SharedReferences中
SharedPreferences.Editor editor = PreferenceManager.getSharedPreferences (this).edit();
这基本上得到了一个SharedReferences.Edit
对象,它允许您编辑共享首选项。现在您可以在其中存储内容了
editor.putBoolean ("checkbox state", <Your boolean value here>);
现在你真的完了
要从共享首选项中获取内容,首先获取共享首选项对象:(不是编辑器
!)
然后您只需调用getXXX
方法:
boolean checkBoxState = prefs.getBoolean ("checkbox state", false);
第一个参数是密钥。您使用的密钥与保存该密钥时使用的密钥相同,即“checkbox state”
。第二个参数是在找不到您要查找的密钥时返回的参数
编辑:
哎呀!我好像忘记了方法的名称。它应该是getdefaultsharedreferences
而不是getsharedreferences
。我的错
基本上可以检查布尔变量checkBoxState
,查看是否禁用单选按钮
if (checkBoxState) {
//If it is true, disable the radiobuttons
<Your radio button name>.setEnabled (false);
}
if(复选框状态){
//如果为真,则禁用单选按钮
.setEnabled(false);
}
//你也可以根据你的需要来决定你要做什么
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();
}
});
如何在您的问题中实现
共享首选项
的总体思路可能会从中清晰可见。最好为代码提供一个解释!我对这个概念还不熟悉,您能提供一个解释吗?我需要用什么来代替“上下文”?这是如何将radiobutton状态与复选框链接起来的?我对conce是新手pt,您还可以提供详细信息,比如我需要在onCreate()中放入哪些部分吗?我收到一个错误。.无法从静态上下文引用非静态方法GetSharedReferences()。(您的第一个代码)我将其放入onCreate()。请参阅我编辑的帖子@HarshwardhanS,在onCreate
中执行我的所有代码都可以
boolean checkBoxState = prefs.getBoolean ("checkbox state", false);
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();
}
});