如何设置 ListPreference 的默认值
我需要在活动启动时设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您不需要以编程方式处理
ListPreferences
的默认值。您可以在 xml 设置文件中执行此操作。下面是一个示例...
这里我选择
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...
here I selected
2
as a default value. Rememberdefaultvalue
will beopts_values
element.你有没有尝试过:
Have you tried:
抱歉我的英语不好。
代码:
Sorry my bad English.
Code:
您可以使用这样的键设置默认值
You can set your default value by using the key like this
或者您也可以尝试
colour.setValue(mycolour);
or you can also try
colour.setValue(mycolour);
仅供记录,如果其他人遇到此问题:
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)
实际上,这是因为 SharedPreferences 在您重新构建应用程序后仍将保留。
卸载它并重试。
Actually it's because the SharedPreferences will persist after you re-build the app.
Uninstall it and try again.
setValue()
是ListPreference的方法,setDefaultvalue
是Preference的方法setValue()
is ListPreference's method, andsetDefaultvalue
is Preference's method这是一篇旧文章,但这是使用以下代码行设置
ListPreference
默认值的另一种方法:This is an old post, but here's another way to set the default value for
ListPreference
with the following line of code:使用列表标记中的 xml 属性
android:defaultValue=""
来设置默认值。如果仍然不起作用,请尝试以下步骤。
奇怪,我知道,但它适用于我的情况。
Use the xml attribute
android:defaultValue="<VALUE>"
in your list tag to set the default value.If still its not working, try below steps.
Strange, I know but it worked in my case.
如果您使用 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
res/values/arrays
res/strings.xml
结果:
设置片段的默认列表值为“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
indata/data/shared_prefs/<package_name_in_app_manifest>
. Replace<package_name_in_app_manifest>
with real name likecom.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
res/values/arrays
res/strings.xml
Result:
Setting Fragment with default list value of "Integrated" given by string resource @string/default_mic