本文介绍了如何设置的目录preference默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在活动开始时设置defult值列表preference。我试着目录preference.setDefaultvalue(价值); ,但它使firts表为默认的输入。我需要它,因为我必须检查的条件,并设置为默认满足该条件的值,所以我觉得它不能从XML文件中完成(与安卓设置defaultValue

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)

例如,假设我有值这个阵列中的arrays.xml:

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>

在preferenceScreen的xml:

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.

推荐答案

您是否尝试过:

setValueIndex(int index);

这篇关于如何设置的目录preference默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:08