如何保存checkbox的状态?

hu_gang 2014-09-02 02:47:42
有两个checkbox控件,选择后记录他们的状态,下次打开时状态还是上次离开时的状态,这要如何实现类?
...全文
490 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshitianca8 2014-09-03
  • 打赏
  • 举报
回复
如果只是存储设置的话,最方便的肯定是SharedPreferences的方式,考虑到同步的话也可以直接传到服务器存
飞奔的豆沙包 2014-09-03
  • 打赏
  • 举报
回复
private SharedPreferences sp; private CheckBox cb_protected; sp = getSharedPreferences("config", Context.MODE_PRIVATE); boolean isProtecting = sp.getBoolean("isProtected", false);//每次进来的时候读取保存的数据 if (isProtecting) { cb_protected.setText("已经开启了保护"); cb_protected.setChecked(true); } cb_protected.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { cb_protected.setText("已经开启保护"); Editor editor = sp.edit(); editor.putBoolean("isProtected", true); editor.commit();//提交数据保存 } else { cb_protected.setText("没有开启保护"); Editor editor = sp.edit(); editor.putBoolean("isProtected", false); editor.commit();//提交数据保存 } } });
PflHighFly 2014-09-03
  • 打赏
  • 举报
回复
放SharedPreferences,需要用的时候就掉用下sp = getSharedPreferences("文件名", MODE_PRIVATE); Stirng a = sp.getString("你存的值", null);
  • 打赏
  • 举报
回复
如果是在当前使用中记住,放静态变量里方便。跳到其他页面再回来,去取下静态变量。 如果要退出应用以后,下次进来还是记住,放SharedPreferences
fireyou 2014-09-02
  • 打赏
  • 举报
回复
SharedPreferences 文件 数据库 都可以
savior120 2014-09-02
  • 打赏
  • 举报
回复
可以读写文件保存保存它
秋杨 2014-09-02
  • 打赏
  • 举报
回复
可以用SharedPreferences保存

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧