我正在使用和引擎为设置创建实时墙纸和共享流程。

这是我的XML文件,它可以托管用户可以为实时墙纸选择的不同设置:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
                android:title="Main Settings">
           <ListPreference
           android:title="Background Image"
           android:summary="Set the background image for the wallpaper"
           android:key="listPref"
           android:defaultValue="1"
           android:entries="@array/background"
           android:entryValues="@array/background_values" /> 
        </PreferenceCategory>
</PreferenceScreen>

我的@array看起来像这样:

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

<string-array name="background">
    <item>Background 1</item>
    <item>Background 2</item>
</string-array>

<string-array name="background_values">
    <item>1</item>
    <item>2</item>
</string-array>

</resources>

我的主要墙纸活动使用此操作来检查用户是否选择了新设置:

    public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
    {            

        if (prefs.getString("listPref", "Background 1").equals(2))
                {
            Toast.makeText(this, "test", Toast.LENGTH_LONG).show();

    final Scene scene = new Scene();
    final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(
                    0, 0, 0, 5);
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(2.7f,
                    new Sprite(0, 0, this.mAutoParallaxCustomBackground)));
    scene.setBackground(autoParallaxBackground);
                }
    }

我的问题是...假设我有10种不同的背景可供选择:背景1,背景2,背景3 ...等,如果像上述方法中的其他陈述一样,我必须继续做才能照顾对于设置中的所有背景?还是我错过了某个地方?必须有一种更简单的方法来弄清楚用户在设置中单击的背景。

我是否必须使用XML文件中的“ background_values”做点事?

是的,我意识到我的话是不正确的,但是我还没有弄清楚如何使其正常工作。

有帮助吗?

解决方案

发现了灵魂:

创建了这个:

    @Override
    public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
    {           
            settingsChanged = true;
    }

添加:

    @Override
    public void onResume(){
        super.onResume();
        if(settingsChanged)
        {
                BuildScene();
                settingsChanged = false;
        }
    }

(buildScene())是我的OnloadScene()方法中的调用。)。

然后,我只是自定义编码,如果elsecene()方法中的其他语句弄清楚用户当前正在使用的选项,然后应用新图像。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top