Java 保存一个;勾选“;启动应用程序时的复选框状态
嗨,我在“保存我的复选框状态”时遇到问题。在我的例子中:选中状态-启用声音,取消选中状态-禁用声音。在my options.xml上,我设置了:Java 保存一个;勾选“;启动应用程序时的复选框状态,java,android,checkbox,state,Java,Android,Checkbox,State,嗨,我在“保存我的复选框状态”时遇到问题。在我的例子中:选中状态-启用声音,取消选中状态-禁用声音。在my options.xml上,我设置了: <CheckBox android:id="@+id/bbutton_sound" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/ebsound" android:layout_m
<CheckBox
android:id="@+id/bbutton_sound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ebsound"
android:layout_marginTop="20.00dp"
android:checked="true"/>
谢谢您的帮助。当您返回时,您的设置会被保存,因此当您启动应用程序时,它会接受未选中的值 试试这个:
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("check");
editor.commit();
}
如果使用内置的首选项XML+活动/片段框架,则选项/菜单设置可能更易于使用和调试。您只需使用预构建的CheckBoxPreference,它将始终从关联的已保存首选项获取其选中状态 在res/xml中:
<CheckBoxPreference
android:key="@string/pref_sounds_key"
android:title="@string/pref_sounds_title"
android:summary="@string/pref_sounds_summary"
android:persistent="true"
android:enabled="true"
android:defaultValue="false"
/>
您好,很抱歉回复太晚,但我找到了一种删除不需要的支票的方法:)。这是一种欺骗,因为我所要做的就是删除游戏内活动的onCreate方法中的状态,这是我游戏中的第一个活动,不能跳过:)
谢谢你的帮助和关心 我认为您的复选框未选中,因为它是您保存在SharedReferences中的最后一个状态,因此代码工作正常。也许您应该扩展应用程序类并将保存在SharedReferences中的属性设置为true,或者在Application.onCreate()方法中删除它。之后每次你的应用程序启动时,你的按钮都应该被检查。哇,这很有效,但正如你所看到的,我得到了返回按钮,它将我引导到主菜单。自动完成选项活动并启动菜单一。“选项”是菜单活动的一部分,当我在“选项”活动中设置onDestroy()方法并保持复选框未选中时,下次输入选项(不退出)时,我将选中复选框。我想的是,我的应用程序包含“闪屏”活动,它位于菜单之前。如果我在那个活动上使用了你的onDestroy方法,它会工作得很好,但是我怎么能做一个引用呢?替换editor.remove(“check”);对于editor.putBoolean(“检查”,true);在onDestroy看一看!那也不行。和我上面提到的效果一样。我还尝试在我的闪屏活动中调用“putBoolean(“check”,true);”,但什么也没有发生,或者我不知道如何实现它;以及onDestroy()方法。现在测试!还注意到您正在使用onClickListener(视图)作为复选框-如果您要这样做,最好使用。
<CheckBoxPreference
android:key="@string/pref_sounds_key"
android:title="@string/pref_sounds_title"
android:summary="@string/pref_sounds_summary"
android:persistent="true"
android:enabled="true"
android:defaultValue="false"
/>
public boolean getPreferenceSounds() {
String key = getString (R.string.pref_sounds_key);
boolean result = getSharedPreferences().getBoolean (key, false);
return result;
}
public void onSharedPreferenceChanged (SharedPreferences sharedPreferences,
String key) { ... }
@Override
public void onCreate(Bundle savedInstanceState) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("check");
editor.commit();