如何在Android中保存复选框状态。 条件:如果选中复选框,则必须禁用radiobuttons,反之亦然。
//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?
}
答案 0 :(得分:0)
//在创建时检查复选框的状态
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);
}
}
});
答案 1 :(得分:0)
您可以执行此操作(假设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);
}
答案 2 :(得分:0)
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
的总体思路可能很清楚。