0

我在使用列表首选项时遇到问题。我正在尝试使用 if 语句将列表首选项值与另一个值匹配

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

条纹主:

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);
            }
.............

设置.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>

形状.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>

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

4

1 回答 1

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);
                }
于 2013-06-26T09:21:02.147 回答