列出Pref麻烦android

时间:2013-06-26 09:08:54

标签: android preference

我在使用list pref时遇到问题。我试图使用if语句

将列表pref值与另一个值匹配

我打赌这没有意义所以我会发布代码......

stripedMain:

String stripeType = "";
void drawStriped() {
            final SurfaceHolder holder = getSurfaceHolder();
            Canvas c = null;
            try {
                c = holder.lockCanvas();
                if (c != null) {
                    if (stripeType == "vert") 
                    {
                    android.util.Log.d("stripedLog", "working");    
                        }
                }
            } finally {
                if (c != null)
                    holder.unlockCanvasAndPost(c);
            }
.............

的settings.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory
        android:key="firKey"
        android:title="Wallpaper Settings" >

        <ListPreference
            android:defaultValue="hor"
            android:entries="@array/stripe_entries"
            android:entryValues="@array/stripe_values"
            android:key="stripe_type"
            android:title="Type" />

    </PreferenceCategory>

</PreferenceScreen>

shapes.xml:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <string-array name="stripe_entries">
        <item>"Horizontal"</item>
        <item>"Vertical"</item>
         <item>"Both"</item>
          <item>"Both+CrossOver"</item>
    </string-array>

    <string-array name="stripe_values">
        <item>"hor"</item>
        <item>"vert"</item>
         <item>"both"</item>
        <item>"bothOver"</item>
    </string-array>

</resources>

这不起作用,我不知道为什么......

1 个答案:

答案 0 :(得分:0)

如果没有达到默认的共享首选项以将其与字符串

进行比较,则无法正常工作

所以你需要做的就是获取它,然后像这样比较:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    String stripeType = prefs.getString("stripe_type", "stripe_type")


    void drawStriped() {
                final SurfaceHolder holder = getSurfaceHolder();
                Canvas c = null;
                try {
                    c = holder.lockCanvas();
                    if (c != null) {
                        if (stripeType.equalsIgnoreCase("vert")) 
                        {
                        android.util.Log.d("stripedLog", "working");    
                            }
                    }
                } finally {
                    if (c != null)
                        holder.unlockCanvasAndPost(c);
                }