如何根据用户在设置中的选择设置新壁纸?
我正在使用 AndEngine 创建动态壁纸和 SharedPreferences 设置。
这是我的 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...等等,我是否必须像上面的方法一样继续使用 If Else 语句才能处理中的所有背景。设置?或者我在某个地方错过了一个步骤?必须有一种更简单的方法来确定用户在设置中单击了哪个背景。
我是否必须对 XML 文件中的“background_values”执行某些操作?
是的,我意识到我的 If 语句是不正确的,但我还没有弄清楚如何让它正常工作。
I'm using AndEngine to create a live wallpaper and SharedPreferences for the settings.
Here is my XML file which hosts the different settings a user can choose for the live wallpaper:
<?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>
My @array looks like this:
<?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>
My main wallpaper activity uses this to check to see if a user has picked a new setting:
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);
}
}
My question is... let's say I have 10 different backgrounds to choose from: Background 1, Background 2, Background 3... etc., will I have to keep making If Else statements like in the above method to be able to care for all the backgrounds in the settings? Or am I missing a step somewhere? There must be an easier way to figure out which background the user has clicked on in the settings.
Do I have to do something with the 'background_values' in my XML file?
And yes, I realize my If statement is incorrect but I haven't yet figured out how to get it to work properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到解决方案:
创建了这个:
添加:
(BuildScene())是我的onLoadScene()方法中的调用。)
然后我只是在BuildScene()方法中自定义编码的If,Else语句来确定用户当前正在使用哪个选项,然后应用新图像。
Found the soulution:
Created this:
Added:
(BuildScene()) is a call inside my onLoadScene() method.)
Then I just custom coded If, Else statements inside the BuildScene() method to figure out which option the user is currently using, and then applied the new image.