[英]Android Studio: Preference android:summary="%s", but with a certain default summary
following problem: I have a dice application which should offer one, two or three dice to use.以下问题:我有一个骰子应用程序,它应该提供一个、两个或三个骰子来使用。 I am currently creating a DropDownPreference to change the value of the number of dice.我目前正在创建一个 DropDownPreference 来更改骰子数量的值。 I always want the current amount of dice to be displayed in the summary using "%s" in the android:summary attribute.我总是希望使用 android:summary 属性中的“%s”在摘要中显示当前的骰子数量。 The problem is, that the default summary entry seems always to be the first value of the "preference_dice" array.问题是,默认摘要条目似乎总是“preference_dice”数组的第一个值。 Is there a way to display a certain default summary entry and afterwards displaying the currently selected amount of dice?有没有办法显示某个默认摘要条目,然后显示当前选择的骰子数量? I need to start with two dice by default.默认情况下,我需要从两个骰子开始。 Thanks!谢谢!
DropDownPreference:下拉首选项:
<DropDownPreference
app:key="@string/preference_dice"
android:title="@string/preference_dice_title"
android:defaultValue="@string/preference_dice_2_value"
android:entries="@array/preference_dice"
android:entryValues="@array/preference_dice_values"
android:summary="%s"
/>
Array for entries:条目数组:
<string-array name="preference_dice">
<item>@string/preference_dice_1</item>
<item>@string/preference_dice_2</item>
<item>@string/preference_dice_3</item>
</string-array>
You need to do it from code.你需要从代码中做到这一点。 Try the following way:尝试以下方式:
DropDownPreference preference = (DropDownPreference) findPreference ("preference_dice");
preference.setDefaultValue(preference_dice_1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.