我一直试图在其他活动中使用/保存复选框的布尔值,但运气不佳。
我知道你必须使用SharedPreferences但是我无法正确设置它。我已经制作了一个有
的偏好类 private static final String OPTION_PREF = "my.main.project";
private SharedPreferences optionPreferences;
private Editor optionEditor;
private boolean checkbox;
public Preferences(Context context)
{
this.optionPreferences = context.getSharedPreferences(OPTION_PREF, Activity.MODE_PRIVATE);
this.optionEditor = optionPreferences.edit();
}
public boolean getChecked()
{
return optionPreferences.getBoolean("is_checked", checkbox);
}
public void saveChecked(boolean checkBox)
{
optionEditor.putBoolean("save_check_pref", checkBox);
optionEditor.commit();
}
然后在选项菜单中,例如
boolean veggieChecked;
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
Preferences pref;
pref = new Preferences(getApplicationContext());
veggieChecked = pref.getChecked();
final CheckBox checkBox = (CheckBox) findViewById(R.id.vegetarian);
if(checkBox.isChecked())
veggieChecked = true;
pref.saveChecked(veggieChecked);
我无法真正看到我做错了什么,因为我是Android的新手并且之前没有使用过共享偏好..任何帮助都将不胜感激!
答案 0 :(得分:1)
乍一看,您正在输入密钥is_checked
,然后尝试提取save_check_pref
。
答案 1 :(得分:1)
您可以使用以下内容:
CheckBox Equities = (CheckBox) findViewById(R.id.Equities);
CheckBox FixedIncome = (CheckBox) findViewById(R.id.FixedIncome);
CheckBox Currencies = (CheckBox) findViewById(R.id.Currencies);
CheckBox Commodities = (CheckBox) findViewById(R.id.Commodities);
CheckBox Derivatives = (CheckBox) findViewById(R.id.Derivatives);
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
prefsEditor = myPrefs.edit();
int prefEq = myPrefs.getInt("Equities", 0);
int prefFI = myPrefs.getInt("FixedIncome", 0);
int prefCu = myPrefs.getInt("Currencies", 0);
int prefCo = myPrefs.getInt("Commodities", 0);
int prefDe = myPrefs.getInt("Derivatives", 0);
if(prefEq == 1)
{
Equities.setChecked(true);
}
else
{
Equities.setChecked(false);
}
if(prefFI == 1)
{
FixedIncome.setChecked(true);
}
else
{
FixedIncome.setChecked(false);
}
if(prefCu == 1)
{
Currencies.setChecked(true);
}
else
{
Currencies.setChecked(false);
}
if(prefCo == 1)
{
Commodities.setChecked(true);
}
else
{
Commodities.setChecked(false);
}
if(prefDe == 1)
{
Derivatives.setChecked(true);
}
else
{
Derivatives.setChecked(false);
}
Equities.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("Equities", 1);
prefsEditor.commit();
}
else
{
myArray[0] = false;
prefsEditor.putInt("Equities", 0);
prefsEditor.commit();
}
}
});
FixedIncome.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("FixedIncome", 1);
prefsEditor.commit();
}
else
{
prefsEditor.putInt("FixedIncome", 0);
prefsEditor.commit();
}
}
});
Currencies.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("Currencies", 1);
prefsEditor.commit();
}
else
{
prefsEditor.putInt("Currencies", 0);
prefsEditor.commit();
}
}
});
Commodities.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("Commodities", 1);
prefsEditor.commit();
}
else
{
prefsEditor.putInt("Commodities", 0);
prefsEditor.commit();
}
}
});
Derivatives.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("Derivatives", 1);
prefsEditor.commit();
}
else
{
prefsEditor.putInt("Derivatives", 0);
prefsEditor.commit();
}
}
});