嗨我设计了一个自定义工具栏,用d出菜单替换 *** 作栏,使用提示
how to save menuitem visibility state through sharedpreferences?
和
Checkbox item state on menu android
和
http://developer.android.com/guide/topics/ui/menus.html#checkable
最有效的方法是将状态存储在共享首选项中,如stackoverflow答案中所示.
我的问题是:即使重新启动我的AndroID后,如何保持选中的选项?
解决方法:
一种方法是在.commit()之前调用.clear()方法.
另一种方法是检索共享首选项中的最后一个存储值.但是,要做到这一点,必须了解活动的生命周期:
存储已检查的用户首选项状态后,如下所示:
声明变量
/**Checkable Login Persist Shared Prefs Declarations Start*/private static final String PREFS_name = "IsCheckedState";String string;SharedPreferences.Editor editor;/**Checkable Login Persist Shared Prefs Declarations End*/
设置布局后,将字符串(无论您的情况可能是boolean或int)变量分配给共享首选项.
protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); /** Persistent Checkable Menu Start **/ SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); string = settings.getString("preference", string); //Log.e("User Subscription", string); /** Persistent Checkable Menu End **/}@OverrIDepublic boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); SharedPreferences settings = this.getSharedPreferences(PREFS_name, 0); string = settings.getString("preference", string); if (string.equals("Vibrate")) { menu.findItem(R.ID.start_action).setChecked(true); Log.e("Vibrate", string); } else if (string.equals("disable")){ menu.findItem(R.ID.my_cancel_action).setChecked(true); Log.e("disable", string); } return true;}// Menu options to set and cancel the alarm.@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { /*persistent checkable item logic start*/ SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); editor = settings.edit(); string = settings.getString("preference", string); /*persistent checkable item logic end*/ int ID = item.getItemID(); // 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. switch (ID) { // When the user clicks START ALARM, set the alarm. case R.ID.start_action: alarm.setAlarm(); item.setChecked(true); string= "Vibrate"; editor.putString("preference", string); editor.commit(); // invalIDateOptionsMenu(); return true; // When the user clicks CANCEL ALARM, cancel the alarm. case R.ID.my_cancel_action: alarm.cancelAlarm(this, 1); item.setChecked(true); string="disable"; editor.putString("preference", "disable"); editor.commit(); //invalIDateOptionsMenu(); return true; } return super.onoptionsItemSelected(item);}
当应用程序暂停,停止,恢复可见(启动)时,您必须从共享首选项中检索以前存储的字符串.因此,除了上面的代码,您还需要添加以下内容:
}@OverrIDepublic voID onResume() { super.onResume(); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); string = settings.getString("preference", string);}@OverrIDeprotected voID onStart() { super.onStart(); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); string = settings.getString("preference", string);}@OverrIDeprotected voID onRestart() { super.onRestart(); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); string = settings.getString("preference", string);}@OverrIDeprotected voID onPause() { super.onPause(); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); string = settings.getString("preference", string);}@OverrIDeprotected voID onStop() { super.onStop(); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); string = settings.getString("preference", string);}
也就是说,如果您确实希望始终存储已选中的选项.
总结以上是内存溢出为你收集整理的java – 重启Android后自定义小部件中的Android持久性可检查菜单全部内容,希望文章能够帮你解决java – 重启Android后自定义小部件中的Android持久性可检查菜单所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)