在Android中使用列表首选项

我有一个文本到语音应用程序,用户可以选择一种语言,并选择男性或女性的声音.问题是,对于每种语言,有不同的字符串用于调用男性和女性的声音,但在我看来,我只有两种选择(男性和女性).

<string-array name="Language">
    <item>English (US)</item>
    <item>English (UK)</item>
    <item>French (France)</item>
    <item>Spanish (Spain)</item>
    <item>Italian</item>
</string-array>

<string-array name="languageAlias">
    <item>"en-US"</item>
    <item>"en-GB"</item>
    <item>"fr-FR"</item>
    <item>"es-ES"</item>
    <item>"it-IT"</item>
</string-array>

<string-array name="Voice">
    <item>Male</item>
    <item>Female</item>
</string-array>

<string-array name="VoiceAlias">
    <item>"usenglishmale"</item>
    <item>"usenglishfemale"</item>
    <item>"ukenglishmale"</item>
    <item>"ukenglishfemale"</item>
    <item>"eurfrenchmale"</item>
    <item>"eurfrenchfemale"</item>
    <item>"eurspanishmale"</item>
    <item>"eurspanishfemale"</item>
    <item>"euritalianmale"</item>
    <item>"euritalianfemale"</item>        
</string-array>

我试图找到一种方法,只根据选择的语言参考相关的男性和女性语音.是否可以在此处执行此操作,还是必须编写一些代码,根据所选语言更改voiceAlias数组的值?

提前致谢

解决方法:

好的,您可以使用两个ListPreferences和一个OnPreferenceChangeListener来完成此操作.首先是XML:

<ListPreference 
    android:key="language_preference"
    android:title="Language"
    android:entries="@array/Language"
    android:entryValues="@array/languageAlias"/>

<ListPreference 
    android:key="gender_preference"
    android:title="Gender"
    android:entries="@array/Voice"
    android:entryValues="@array/VoiceData"/>

让我们在res / values / array.xml中创建一个新条目:

<string-array name="VoiceData">
    <item>0</item>
    <item>1</item>
</string-array>

现在,在您对PreferenceActivity的扩展中,我们将获取在您的SharedPreferences中持久存在的字符串值,并从中创建一个全新的条目,在SharedPreferences中从“VoiceAlias”获取其值.

SharedPreferences shareprefs = getPreferenceManager().getSharedPreferences();
Resources resources = YourContext.getResources();

private void makeVoiceData() {
    String languageData = shareprefs.getString("language_preference", "en-US");
    int genderData = Integer.parseInt(shareprefs.getString("gender_preference", "0"));
    String[] voiceAlias = resources.getStringArray(R.array.VoiceAlias);

    int a = 0
    String[] languageAlias = resources.getStringArray(R.array.languageAlias);
    for (a ; a < languageAlias.length ; a++) {
        if (languageAlias[a].equals(languageData)) {
            break;
        }
    }

    shareprefs.putString("VoiceAlias", voiceAlias[(2 * a) + genderData]);
}

ListPreference language_preference = getPreference("language_preference");
ListPreference gender_preference = getPreference("gender_preference");

language_preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    public boolean onPreferenceChanged(Preference preference, Object newValue) {
        shareprefs.putString("language_preference", (String) newValue);
        makeVoiceData();
    }
});

gender_preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    public boolean onPreferenceChanged(Preference preference, Object newValue) {
        shareprefs.putString("gender_preference", (String) newValue);
        makeVoiceData();
    }
});
上一篇:android – 处理JSON对象的最佳方法是什么?


下一篇:在Java中管理配置(初始配置/保存/加载配置)