提问人:Jonathan Chiou 提问时间:1/12/2015 最后编辑:CommunityJonathan Chiou 更新时间:1/12/2015 访问量:4117
保存复选框的状态,然后加载它
Saving a checkbox's state, and then loading it
问:
我想做的是,当按下主菜单上的按钮时,设置菜单会拉起(设置菜单是作为与主菜单分开的活动实现的)。为简单起见,假设我的主菜单是空白的,除了 1 个拉起设置菜单的按钮。在设置菜单中,有一个复选框和一个返回主活动的“完成”按钮。
如何保存复选框并加载它(代码应该是什么,我应该把它放在哪里,为什么等等)?我试过谷歌搜索它,并试图复制结果,但我似乎无法得到它。到目前为止发生了两件事:什么都没有保存,或者我的程序崩溃了。
保存有关复选框的信息后,我如何能够从我的主要活动中访问此信息,@I希望能够根据用户是否选中该框来运行某些代码?
我登陆并尝试过的一些结果: 如何保存复选框状态? - android 保存复选框状态
(请记住,我对此完全陌生)
public class Settings extends ActionBarActivity {
private static final String SETTING_CHECK_BOX = "SETTINGS";
private CheckBox cb;
//char boxChecked = '0';
@Override
protected void onCreate(Bundle savedSettings) {
super.onCreate(savedSettings);
cb = (CheckBox) findViewById(R.id.checkBox);
setContentView(R.layout.activity_settings);
cb.setChecked(isCheckedSettingEnabled());
}
private void setCheckedSettingEnabled(boolean enabled) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean(SETTING_CHECK_BOX, enabled).apply();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
private boolean isCheckedSettingEnabled() {
return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(SETTING_CHECK_BOX, false);
}
public void onPause() {
super.onPause();
// Persist the setting. Could also do this with an OnCheckedChangeListener.
setCheckedSettingEnabled(cb.isChecked());
}
public void clickedDone (View v) {
SharedPreferences settings = getSharedPreferences("SETTINGS", 0);
settings.edit().putBoolean("check",true).commit();
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
所以现在我的应用程序不再崩溃,但状态不会被记住(打开设置菜单时始终未选中)。我将cb.setChecked(checkState)更改为cb.setChecked(TRUE),它没有改变任何内容(当设置菜单打开时仍然始终未选中)。这是怎么回事?
答:
一旦状态数据被存储,系统就会使用 来重新创建状态。onSaveInstanceState()
onRestoreInstanceState(Bundle savedInstanceState)
如果您使用的是 onSaveInstanceState(),则必须在活动中覆盖函数 onRestoreInstanceState() 或在 onCreate() 中使用保存的状态。我认为最好使用 onRestoreInstanceState() ,它可以减少 onCreate() 中的代码,因为 onSavedInstanceSate() 仅在系统 关闭应用以为运行新应用程序腾出空间。如果您只想保存选中的状态,请使用共享首选项,不需要 onSaveInstanceState()。 课堂上的贴花
private CheckBox cb;
private SharedPreferences preferences ;
private SharedPreferences.Editor editor;
private boolean CHECKED_STATE;
@Override
protected void onCreate(Bundle savedSettings) {
super.onCreate(savedSettings);
setContentView(R.layout.activity_settings);
preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
editor = preferences.edit();
CHECKED_STATE = preferences.getBoolean("check", false);
cb = (CheckBox) findViewById(R.id.checkBox);
cb.setChecked(CHECKED_STATE);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
editor.putBoolean("check", isChecked);
editor.commit();
}
});
}
this code saves the state on clicking the check box.
要使其在“后按”时保存状态,请将以下内容添加到您的活动中。
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
editor.putBoolean("check", cb.isChecked());
editor.commit();
}
有关保存实例状态的更多详细信息,请参阅此链接。
评论
onSaveInstanceState()
仅用于保存该实例的数据。一旦调用,该状态就不再相关。您需要将设置写入持久性存储。适合您案例的简单存储解决方案是 .Activity
Activity
finish()
SharedPreferences
public class Settings extends ActionBarActivity {
// Create a constant for the setting that you're saving
private static final String SETTING_CHECK_BOX = "checkbox_setting";
private CheckBox mCheckBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mCheckBox = (CheckBox) findViewById(R.id.checkBox);
// Set the initial state of the check box based on saved value
mCheckBox.setChecked(isCheckedSettingEnabled());
}
@Override
public void onPause() {
super.onPause();
// Persist the setting. Could also do this with an OnCheckedChangeListener.
setCheckedSettingEnabled(mCheckBox.isChecked());
}
/**
* Returns true if the setting has been saved as enabled,
* false by default
*/
private boolean isCheckedSettingEnabled() {
return PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(SETTING_CHECK_BOX, false);
}
/**
* Persists the new state of the setting
*
* @param enabled the new state for the setting
*/
private void setCheckedSettingEnabled(boolean enabled) {
PreferenceManager.getDefaultSharedPreferences(this)
.edit()
.putBoolean(SETTING_CHECK_BOX, enabled)
.apply();
}
}
评论
setContentView()
findViewById()
setContentView()
findViewById()
null
setChecked()
下一个:列出意向的所有附加功能
评论