0

如何创建一个简单的单选按钮首选项来选择在 BATTERY_LOW 条件下使用哪种 LED 颜色?

4

1 回答 1

0

要更改 LED 颜色: 检查此链接

对于 BATTERY_LOW,您将需要一个广播接收器: 检查此链接

好的。

首先,在 values 文件夹中创建一个文件。称之为“preference_res.xml”。然后添加以下内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array
    name="prefColor">
    <item>None</item>
    <item>Blue</item>
    <item>Green</item>
    <item>Yellow</item>
    <item>Other Color</item>
</string-array>
<string-array
    name="prefColorVal">
    <item>-1</item>
    <item>-2</item>
    <item>79</item>
    <item>24</item>
    <item>25</item>
    <item>27</item>
    <item>80</item>
</string-array>
 </resources>

(请注意,prefColorVal 项应引用您希望单选按钮具有的值。此处显示的值是随机的。)

在您的preference.xml 中,添加如下内容:

<ListPreference
    android:dialogTitle="Choose color"
    android:entries="@array/prefColor"
    android:entryValues="@array/prefColorVal"
    android:key="bat_below_15"
    android:negativeButtonText="Cancel"
    android:positiveButtonText="Save"
    android:summary="Choose color when below 15"
    android:title="Color when below 15" />

如果你不知道如何获取或使用这些值,这里有一些提示,但你应该用谷歌搜索这些东西:

preferences = PreferenceManager.getDefaultSharedPreferences(ctx);

并使用它来获取值:

  preferences.getString("bat_below_15", Blue));

希望能帮助到你。

于 2013-07-22T15:11:02.090 回答