如何在销毁活动后保持相机的复选框状态和设置?
或者换句话说,如何将相机参数保存到SharedPreferences中?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_old);
SharedPreferences preferences =
getSharedPreferences("PREFS_NAME",MODE_WORLD_READABLE);
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
yourCheckBox.setChecked(preferences.getBoolean("lol",false));
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton yourCheckBox,
boolean isChecked) {
if (isChecked){
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NEGATIVE);
camera.setParameters(params);
}
else {
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NONE);
camera.setParameters(params);
}
}
});
public void onStop(){
SharedPreferences settings = getSharedPreferences("PREFS_NAME", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("lol", true);
editor.commit();
super.onStop();
}
如你所说,不想保存设置! :(
SharedPreferences preferences = getSharedPreferences("qwe",MODE_PRIVATE);
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
yourCheckBox.setChecked(preferences.getBoolean("lol",false));
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton yourCheckBox,
boolean isChecked) {
if (isChecked){
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NEGATIVE);
camera.setParameters(params);
}
else {
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NONE);
camera.setParameters(params);
}
}
});
protected void onPause()
{
SharedPreferences settings = getSharedPreferences("qwe",MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
editor.putBoolean("lol", yourCheckBox.isChecked());
editor.commit();
super.onPause();
}
答案 0 :(得分:0)
不是总是在SharedPreferences中存储“true”,而只是存储CheckBox的当前状态。
SharedPreferences settings = getSharedPreferences("PREFS_NAME", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
CheckBox yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
editor.putBoolean("lol", yourCheckBox.isChecked());
editor.commit();
您也可以考虑将此代码移至onPause()而不是onStop()。操作系统(pre-Honeycomb)可以暂停您的活动,而无需调用onStop()。如果发生这种情况,您的SharedPreferences将不会被保存。