gpt4 book ai didi

java - 重启 Android 后自定义小部件中的 Android 持久可检查菜单

转载 作者:行者123 更新时间:2023-11-30 01:22:13 24 4
gpt4 key购买 nike

您好,我使用来自

的提示设计了一个自定义工具栏,用弹出菜单替换操作栏

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()方法。

另一种是检索共享首选项中最后存储的值。但是,要做到这一点,必须了解 Activity 的生命周期

存储用户偏好的勾选状态后,如下:声明变量

/**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 值或整数,无论​​您的情况如何)分配给共享首选项。

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
**/}
@Override
public 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.
@Override
public 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);
}

当应用程序暂停、停止、恢复可见(启动)时,您必须从共享首选项中检索先前存储的字符串。因此,除了上面的代码之外,您还需要添加以下内容:

}
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}

@Override
protected void onStart() {
super.onStart();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}

@Override
protected void onRestart() {
super.onRestart();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}


@Override
protected void onPause() {
super.onPause();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}

也就是说,如果您真的希望始终存储选中的选项。

关于java - 重启 Android 后自定义小部件中的 Android 持久可检查菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36939380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com