我已经制作了4个单选按钮,并希望在单击任何一个时保存状态,然后想要在应用程序中使用该已保存的状态。我该怎么办?
myOption1.setChecked(true);
myOption2.setChecked(true);
myOption3.setChecked(true);
myOption4.setChecked(true);
答案 0 :(得分:4)
如果你只有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));
答案 1 :(得分:4)
在您的活动中覆盖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"));
}
答案 2 :(得分:0)
将该值保存在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);