我正在尝试使用android偏好设置api保存我的应用程序的某些配置。
是否可以使用string.xml
中的值作为首选项的键?例如:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_sms_storage_title"
android:key="@string/pref1">
<CheckBoxPreference
android:key="@string/pref2"
android:summary="@string/pref_summary_auto_delete"
android:title="@string/pref_title_auto_delete"
android:defaultValue="false" />
</PreferenceCategory>
</PreferenceScreen>
当我尝试读取以下值时:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Boolean b = preferences.getBoolean(getString(R.string.pref2),false);
即使将其设置为false
,它也始终返回true
我注意到,如果我将文字值放在key
字段中,则它的工作方式如下:
<CheckBoxPreference
android:key="something"
android:summary="@string/pref_summary_auto_delete"
android:title="@string/pref_title_auto_delete"
android:defaultValue="false" />
Boolean b = preferences.getBoolean("something",false);
此案例b
会返回true
使用首选项API将粘贴字符串文字复制到preferences.xml的官方方法是吗?
难道没有其他方式更容易出错吗?
答案 0 :(得分:0)
尝试以下简单示例:
1)MnnnnActivity.class:-----------------
public class MnnnnActivity extends AppCompatActivity {
private Button b;
private TextView tv;
private SharedPreferences preferences;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout4);
String preferenceName = getResources().getString(R.string.pref_key);
preferences = getApplicationContext().getSharedPreferences(preferenceName,
0);
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MnnnnActivity.this , prefAct.class);
startActivity(i);
}
});
tv = (TextView) findViewById(R.id.tv);
}
@Override
protected void onResume() {
super.onResume();
if(preferences != null && tv != null){
tv.setText("Changed to " + String.valueOf(preferences.getBoolean(getResources().getString(R.string.pref2) , false)));
}
}
}
2)prefAct.class:--------
public class prefAct extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
EditPrefs mEditPrefs= new EditPrefs();
mFragmentTransaction.replace(android.R.id.content, mEditPrefs);
mFragmentTransaction.commit();
}
}
3)EditPrefs.class:------------
public class EditPrefs extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private int preferencesToEdit;
private PreferenceHelper sensor_preferences;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String preferenceName = getResources().getString(R.string.pref_key);
preferencesToEdit = R.xml.pr;
PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.setSharedPreferencesName(preferenceName);
preferenceManager.setSharedPreferencesMode(0);
getActivity().setTitle(getResources().getString(R.string.pref_sensor_action_bar_title));// TODO:change this
addPreferencesFromResource(preferencesToEdit);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (findPreference(key) != null) {
if (findPreference(key).getKey().equals(getResources().getString(R.string.pref2))) {
Toast.makeText(getActivity(), "Changed to " + sharedPreferences.getBoolean(getResources().getString(R.string.pref2), false), Toast.LENGTH_LONG).show();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
if(view != null) {
view.setBackgroundColor(getResources().getColor(android.R.color.white));
}
return view;
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
}
4)layout4.xml:--------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_gravity="center"
android:text="Click"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/tv"
android:text="x"/>
</LinearLayout>
5)pr.xml:---------------
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<CheckBoxPreference
android:key="@string/pref2"
android:summary="@string/pref_summary_auto_delete"
android:title="@string/pref_title_auto_delete"
android:defaultValue="false" />
</PreferenceCategory>
</PreferenceScreen>
6)字符串资源:--------
<string name="pref_key">pref_key</string>
<string name="pref2">pref2</string>
<string name="pref_title_auto_delete">title</string>
<string name="pref_summary_auto_delete">summary</string>
7)不要忘记在清单中同时包含这两个活动。