当用户退出应用程序时,如何保存 Android CheckBox 的状态?
当用户退出应用程序时,有什么方法可以保存复选框的状态(选中或未选中),以便我可以在应用程序重新启动时重新加载此状态?
@Override
public void onPause()
{
super.onPause();
save(itemChecked);
}
@Override
public void onResume()
{
super.onResume();
checkOld = load();
for (int i = 0 ; i < checkOld.length; i++)
{
notes.ctv.get(i).setChecked(checkOld[i]);
}
}
@Override
public void onRestart()
{
super.onResume();
checkOld = load();
for (int i = 0 ; i < checkOld.length; i++)
{
notes.ctv.get(i).setChecked(checkOld[i]);
}
}
private void save(final boolean[] isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
insertState();
for(Integer i = 0; i < isChecked.length; i++)
{
editor.putBoolean(i.toString(), isChecked[i]);
}
editor.commit();
}
private boolean[] load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
boolean [] reChecked = new boolean[itemChecked.length];
for(Integer i = 0; i < itemChecked.length; i++)
{
reChecked[i] = sharedPreferences.getBoolean(i.toString(), false);
}
return reChecked;
}
Is there any way by which I can save the state of my checkboxes (checked or unchecked) when user exits the application so that I can reload this state when the application restarts?
@Override
public void onPause()
{
super.onPause();
save(itemChecked);
}
@Override
public void onResume()
{
super.onResume();
checkOld = load();
for (int i = 0 ; i < checkOld.length; i++)
{
notes.ctv.get(i).setChecked(checkOld[i]);
}
}
@Override
public void onRestart()
{
super.onResume();
checkOld = load();
for (int i = 0 ; i < checkOld.length; i++)
{
notes.ctv.get(i).setChecked(checkOld[i]);
}
}
private void save(final boolean[] isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
insertState();
for(Integer i = 0; i < isChecked.length; i++)
{
editor.putBoolean(i.toString(), isChecked[i]);
}
editor.commit();
}
private boolean[] load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
boolean [] reChecked = new boolean[itemChecked.length];
for(Integer i = 0; i < itemChecked.length; i++)
{
reChecked[i] = sharedPreferences.getBoolean(i.toString(), false);
}
return reChecked;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
组合
onPause()
和onResume()
来保存和加载CheckBox
值。示例代码:
Combine
onPause()
andonResume()
to save and load yourCheckBox
value.Sample code:
我相信 google notepad3 教程解释了如何保存和恢复状态 http:// developer.android.com/resources/tutorials/notepad/notepad-ex3.html
将状态包保存在 onSaveInstanceState() 中,然后在 onStart() 中取回该包
希望有所帮助
编辑:另请检查这个,比较简洁。
onSaveInstanceState () 和 onRestoreInstanceState ()
i believe the google notepad3 tutorial explains about saving and restore state http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html
save the state bundle in onSaveInstanceState() then get the bundle back in onStart()
Hope that helps
Edit: also check this one, its more concise.
onSaveInstanceState () and onRestoreInstanceState ()