如何设置 ListPreference 的默认值

发布于 2024-10-20 08:36:17 字数 1142 浏览 7 评论 0原文

我需要在活动启动时设置 ListPreference 的默认值。 我尝试过使用 ListPreference.setDefaultvalue("value"); 但它使列表的第一个条目成为默认值。我需要它,因为我必须检查一个条件并将满足该条件的值设置为默认值,所以我认为它不能从 xml 文件中完成(使用 android:defaultValue

例如,假设我在 arrays.xml 中有这个值数组:

<string-array name="opts">
    <item>red</item>
    <item>green</item>
    <item>blue</item>
</string-array>

<string-array name="opts_values">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>

在 PreferenceScreen xml 中:

<ListPreference
    android:title="Colour select"
    android:summary="Select your favourite"
    android:key="colour"
    android:entries="@array/opts"
    android:entryValues="@array/opts_values" />

在 Activity 中我想做这样的事情:

String mycolour;
if (something) {
    mycolour="1";
} else {
    mycolour="2";
}
ListPreference colour = (ListPreference) findPreference ("colour");
colour.setDefaultValue(mycolour);

但它不起作用,因为它将第一个选择作为默认值。您能解释一下如何将另一项设置为默认值吗?谢谢。

i need to set the defult value for a ListPreference when the Activity starts.
I've tried with ListPreference.setDefaultvalue("value"); but it makes the firts Entry of the List as default. I need it because i must check a condition and set as default the value which satisfies that condition, so I think it can't be done from the xml file (with android:defaultValue)

For example, suppose I have this array of values in the arrays.xml:

<string-array name="opts">
    <item>red</item>
    <item>green</item>
    <item>blue</item>
</string-array>

<string-array name="opts_values">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>

In the PreferenceScreen xml:

<ListPreference
    android:title="Colour select"
    android:summary="Select your favourite"
    android:key="colour"
    android:entries="@array/opts"
    android:entryValues="@array/opts_values" />

In the Activity I'd like to do something like this:

String mycolour;
if (something) {
    mycolour="1";
} else {
    mycolour="2";
}
ListPreference colour = (ListPreference) findPreference ("colour");
colour.setDefaultValue(mycolour);

But it doesn't work, because it makes the first choice as default. Could you explain me how to make another one as default? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

纵情客 2024-10-27 08:36:17

您不需要以编程方式处理 ListPreferences 的默认值。您可以在 xml 设置文件中执行此操作。下面是一个示例

<string-array name="opts">
    <item>red</item>
    <item>green</item>
    <item>blue</item>
</string-array>

<string-array name="opts_values">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>

...

<ListPreference
    android:title="Colour select"
    android:summary="Select your favourite"
    android:key="colour"
    android:entries="@array/opts"
    android:entryValues="@array/opts_values"
    android:defaultValue="2" />

这里我选择 2 作为默认值。请记住 defaultvalue 将是 opts_values 元素。

You don't need to programmatically handle the default value of ListPreferences. You can do this in xml setting file. Below is an example

<string-array name="opts">
    <item>red</item>
    <item>green</item>
    <item>blue</item>
</string-array>

<string-array name="opts_values">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>

...

<ListPreference
    android:title="Colour select"
    android:summary="Select your favourite"
    android:key="colour"
    android:entries="@array/opts"
    android:entryValues="@array/opts_values"
    android:defaultValue="2" />

here I selected 2 as a default value. Remember defaultvalue will be opts_values element.

姜生凉生 2024-10-27 08:36:17

你有没有尝试过:

setValueIndex(int index);

Have you tried:

setValueIndex(int index);
不顾 2024-10-27 08:36:17

抱歉我的英语不好。

  1. 列表项
  2. 检索列表 检查值是否为空。如果为空则设置为默认值。

代码:

ListPreference dataPref = (ListPreference) findPreference("keyList");

if(dataPref.getValue() == null){
    dataPref.setValueIndex(0); //set to index of your deafult value
}

Sorry my bad English.

  1. List item
  2. Retrieve the list Check if the value is null. If it is null set to the default value.

Code:

ListPreference dataPref = (ListPreference) findPreference("keyList");

if(dataPref.getValue() == null){
    dataPref.setValueIndex(0); //set to index of your deafult value
}
后eg是否自 2024-10-27 08:36:17

您可以使用这样的键设置默认值

<string-array name="syncFrequency">
    <item name="1">Block All Calls</item>
    <item name="2">Block Black List</item>
    <item name="3">Block Unknown Calls</item>
    <item name="4">Allow White List</item>
    <item name="5">Receive All Calls</item>
</string-array>




<string-array name="syncFrequencyValues">
    <item name="1">Block_All_Calls</item>
    <item name="2">Block_Black_List</item>
    <item name="3">Block_Unknown_Calls</item>
    <item name="4">Allow_White_List</item>
    <item name="5">Receive_All_Calls</item>
</string-array>



     <ListPreference
        android:key="prefSyncFrequency"
        android:entries="@array/syncFrequency"
        android:summary="%s"
        android:defaultValue="Block_Black_List"
        android:entryValues="@array/syncFrequencyValues"
        android:title="@string/call_block_options" />

You can set your default value by using the key like this

<string-array name="syncFrequency">
    <item name="1">Block All Calls</item>
    <item name="2">Block Black List</item>
    <item name="3">Block Unknown Calls</item>
    <item name="4">Allow White List</item>
    <item name="5">Receive All Calls</item>
</string-array>




<string-array name="syncFrequencyValues">
    <item name="1">Block_All_Calls</item>
    <item name="2">Block_Black_List</item>
    <item name="3">Block_Unknown_Calls</item>
    <item name="4">Allow_White_List</item>
    <item name="5">Receive_All_Calls</item>
</string-array>



     <ListPreference
        android:key="prefSyncFrequency"
        android:entries="@array/syncFrequency"
        android:summary="%s"
        android:defaultValue="Block_Black_List"
        android:entryValues="@array/syncFrequencyValues"
        android:title="@string/call_block_options" />
開玄 2024-10-27 08:36:17

或者您也可以尝试colour.setValue(mycolour);

or you can also try colour.setValue(mycolour);

迷爱 2024-10-27 08:36:17

仅供记录,如果其他人遇到此问题:

setValueIndex(int X) 正在将值@index X 设置为默认值 - 所以它是可能您正在寻找的内容

添加值之后设置此值! (愚蠢的错误,但花了我半个小时)

Just for the record if someone else has this problem:

setValueIndex(int X) is setting the value @ index X to the default value - so it is probably what you are looking for.

Set this value AFTER you added the Values! (stupid mistake but took me half an hour)

娜些时光,永不杰束 2024-10-27 08:36:17

实际上,这是因为 SharedPreferences 在您重新构建应用程序后仍将保留。
卸载它并重试。

Actually it's because the SharedPreferences will persist after you re-build the app.
Uninstall it and try again.

貪欢 2024-10-27 08:36:17
((ListPreference) findPreference("pref_language")).setValue(Locale
                .getDefault().getLanguage());

setValue()是ListPreference的方法,setDefaultvalue是Preference的方法

((ListPreference) findPreference("pref_language")).setValue(Locale
                .getDefault().getLanguage());

setValue() is ListPreference's method, and setDefaultvalue is Preference's method

稳稳的幸福 2024-10-27 08:36:17

这是一篇旧文章,但这是使用以下代码行设置 ListPreference 默认值的另一种方法:

PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);

This is an old post, but here's another way to set the default value for ListPreference with the following line of code:

PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
空城之時有危險 2024-10-27 08:36:17

使用列表标记中的 xml 属性 android:defaultValue="" 来设置默认值。

注意:是实际值而不是字符串的索引
数组。

如果仍然不起作用,请尝试以下步骤。

  • 清除应用程序数据。
  • 卸载并重新安装应用程序
  • 检查列表首选项,您将看到默认值选择

奇怪,我知道,但它适用于我的情况。

Use the xml attribute android:defaultValue="<VALUE>" in your list tag to set the default value.

Note: the <VALUE> is the actual value and not the index of string
array.

If still its not working, try below steps.

  • Clear application data.
  • Uninstall and reinstall the app
  • Check the list preference, you will see the default value selected

Strange, I know but it worked in my case.

勿忘初心 2024-10-27 08:36:17

如果您使用 Android Jetpack Preference(在 Kotlin 中称为 androidx.preference:preference-ktx:1.1.1),则可以使用:

app:defaultValue=""

另外:defaultValue不是索引号,而是values数组中的实际值。

我还建议使用字符串资源作为默认值,并清除数据、卸载应用或删除文件:

  • _preferences.xml数据/数据/shared_prefs/。将 替换为真实姓名,例如 com.example.yourapp

我遇到了同样的问题,并且 defaultValue 没有更新,因为它已经有一个错误的默认值“true”。通过使用 Android Studio 文件资源管理器并删除该文件解决了这个问题。

这是我的解决方案示例:

res/xml/root_preferences.xml

<ListPreference
    
    app:key="mic"
    
    app:title="@string/select_mic_title"
 
    app:useSimpleSummaryProvider="true"
   
    app:entries="@array/mic_entries"

    app:entryValues="@array/mic_values"
    app:defaultValue="@string/default_mic"
    />

res/values/arrays

<!-- Mic Preference -->

<string-array name="mic_entries">

    <item>@string/default_mic</item>

    <item>Headband</item>

</string-array>


<string-array name="mic_values">

    <item>@string/default_mic</item>

    <item>Headband</item>

</string-array>

res/strings.xml

<string name="default_mic">Integrated</string>

结果:
设置片段的默认列表值为“Integrated”,由字符串资源@string/default_mic给定

If you are using Android Jetpack Preference, known as androidx.preference:preference-ktx:1.1.1 in Kotlin, you can use:

app:defaultValue="<Value_in_string-array_with_values>".

Additionally: defaultValue is not an index number, is the actual value from the values array.

I also recommend using a string resource for the default value and clearing the data, uninstalling the app, or removing the file:

  • <package_name_in_app_manifest>_preferences.xml in data/data/shared_prefs/<package_name_in_app_manifest>. Replace <package_name_in_app_manifest> with real name like com.example.yourapp.

I was having the same issue and the defaultValue wasn't updating because it already had a wrong default value of "true". Solved it by using the Android Studio File Explorer and deleting the file.

And here is my solution example:

res/xml/root_preferences.xml

<ListPreference
    
    app:key="mic"
    
    app:title="@string/select_mic_title"
 
    app:useSimpleSummaryProvider="true"
   
    app:entries="@array/mic_entries"

    app:entryValues="@array/mic_values"
    app:defaultValue="@string/default_mic"
    />

res/values/arrays

<!-- Mic Preference -->

<string-array name="mic_entries">

    <item>@string/default_mic</item>

    <item>Headband</item>

</string-array>


<string-array name="mic_values">

    <item>@string/default_mic</item>

    <item>Headband</item>

</string-array>

res/strings.xml

<string name="default_mic">Integrated</string>

Result:
Setting Fragment with default list value of "Integrated" given by string resource @string/default_mic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文