java - 从 ListPreference 获取 EntryValue 并设置更改

标签 java android listpreference

我不明白如何从 ListPreference 获取 entryValue。我想为我的应用程序设置本地化。但该代码必须在setContentView(R.layout.activity_main);之前设置。简而言之,我的应用程序是一个多语言应用程序。我应该怎么办?以及如何从 ListPreference 获取 entryValue

这是我的代码:

列表首选项

<ListPreference android:key="pref.language"
            android:title="@string/language"
            android:dialogTitle="@string/select_language"
            android:entries="@array/pref_language_entries"
            android:entryValues="@array/pref_language_values"
            android:defaultValue="en_US"
            android:summary="%s" />

strings.xml

<string-array name="pref_language_entries">
    <item>English</item>
    <item>Indonesian</item>
    <item>Russian</item>
</string-array>

<string-array name="pref_language_values">
    <item>en_US</item>
    <item>in_ID</item>
    <item>ru_RU</item>
</string-array>

据我所知,此代码用于更改 Activity 的区域设置:

Locale locale = new Locale("entryValue goes here");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                  getBaseContext().getResources().getDisplayMetrics());

感谢您的努力...

最佳答案

为什么......这非常简单:

listPref.getValue() 应该可以获取当前选定的条目值,前提是您实际上已从 PreferenceScreen 获取了 ListPreference 的引用。

ListPreference documentation link .

Link to a simple example .

关于java - 从 ListPreference 获取 EntryValue 并设置更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26163574/

相关文章:

java - 当类中没有定义泛型类型变量时处理泛型类成员

java - 我们如何从 Instagram 获取访问 token

java - 在jOOq中,为什么连接与语句构造高度耦合?

java - 是什么导致我的二维数组发生变化?

android - 权限拒绝 : broadcasting intent android?

android - 如何将 Sqlite 数据库添加到我的项目中?

java - Libgdx - 未知位置的 IllegalStateException

android - EditTextPreference 值仅在第二次单击 ListPreference 后刷新

android - 如何使首选项不保存到 SharedPreferences?

Android ListPreference 返回错误值