我制作了 4 个单选按钮,并希望在单击其中任何一个按钮时保存状态,然后想在应用程序中使用该保存的状态。我该怎么做?
myOption1.setChecked(true);
myOption2.setChecked(true);
myOption3.setChecked(true);
myOption4.setChecked(true);
我制作了 4 个单选按钮,并希望在单击其中任何一个按钮时保存状态,然后想在应用程序中使用该保存的状态。我该怎么做?
myOption1.setChecked(true);
myOption2.setChecked(true);
myOption3.setChecked(true);
myOption4.setChecked(true);
在您的活动中覆盖onSaveInstanceState()和onRestoreInstanceState()。
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("myOption1", myOption1.isChecked());
savedInstanceState.putBoolean("myOption2", myOption2.isChecked());
savedInstanceState.putBoolean("myOption3", myOption3.isChecked());
savedInstanceState.putBoolean("myOption4", myOption4.isChecked());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myOption1.setChecked(savedInstanceState.getBoolean("myOption1"));
myOption2.setChecked(savedInstanceState.getBoolean("myOption2"));
myOption3.setChecked(savedInstanceState.getBoolean("myOption3"));
myOption4.setChecked(savedInstanceState.getBoolean("myOption4"));
}
如果您总共只有 4 个单选按钮,您不妨将它们的“值”存储在SharedPreferences
(持久存储)中。
例子:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor shEditor = sharedPreferences.edit();
shEditor.putBoolean("checkbox_1", myOptionOne.isChecked());
shEditor.putBoolean("checkbox_2", myOptionTwo.isChecked());
shEditor.putBoolean("checkbox_3", myOptionThree.isChecked());
shEditor.putBoolean("checkbox_4", myOptionFour.isChecked());
shEditor.commit();
通过执行以下操作来使用它:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
myOptionOne.setChecked(sharedPreferences.getBoolean("checkbox_1", false));
myOptionTwo.setChecked(sharedPreferences.getBoolean("checkbox_2", false));
myOptionThree.setChecked(sharedPreferences.getBoolean("checkbox_3", false));
myOptionFour.setChecked(sharedPreferences.getBoolean("checkbox_4", false));
将该值保存在 sharedPreferences 概念中。示例示例已放置在此处,该示例使用名为 addPreferencesFromResource() 的预定义方法,该方法将自动获取复选框值的状态并保存在首选项中。
public class EditPreference extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.checkboxpref);
addPreferencesFromResource(R.layout.checkboxpref);
}
}
布局代码应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="@string/checkbox_pref"
android:title="@string/eidt_preferences">
<CheckBoxPreference
android:key="@string/pref_12_time_format"
android:title="@string/time_formats"
android:summary="@string/twelve_hour"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/pref_show_current_location"
android:title="@string/show_current_location"
android:summary="@string/using_gps_network"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/pref_display_date"
android:title="@string/display_date"
android:defaultValue="true"/>
</PreferenceScreen>
现在您已经检索到此存储的复选框值如下:
SharedPreferences sharedPreference = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
mTimeFormat = sharedPreference.getBoolean("pref_12_time_format", false);
mServiceType = sharedPreference.getBoolean(
"pref_show_current_location", false);
mDisplayDate = sharedPreference.getBoolean("pref_display_date", false);