gpt4 book ai didi

Android 在 preferences.xml 文件中使用字符串资源

转载 作者:行者123 更新时间:2023-11-29 02:28:21 27 4
gpt4 key购买 nike

我正在尝试使用 android preferences 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 吗?

有没有其他更不容易出错的方法?

最佳答案

试试下面这个简单的例子:

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) 不要忘记在 list 中包含这两个 Activity 。

关于Android 在 preferences.xml 文件中使用字符串资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980224/

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