将 LibGDX 与 Android 首选项一起使用
我正在尝试将 Andrioid 的首选项系统与 LibGDX 的首选项系统结合使用.他们都使用 SharedPreferences 作为后端,所以我认为他们应该能够一起工作,但是当我尝试在 LibGDX 的首选项中加载数据时,我没有得到任何数据.
I'm trying to use Andrioid's preferences system in conjunction with LibGDX's preferences system. They both use SharedPreferences as a backend, so I figure they should be able to work together, but when I try to load the data in LibGDX's preferences, I don't get any data back.
我的 Androidpreferences.xml 文件(我知道它很短,稍后会有更多内容:P):
My Android preferences.xml file (I know it's short, it'll have much more later :P):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="framerate"
android:title="Set Framerate"
android:enabled="true"
android:persistent="true"
android:defaultValue="25" />
</PreferenceScreen>
这是我的 PreferenceActivity:
Here is my PreferenceActivity:
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class WallpaperSettings extends PreferenceActivity {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < 11) {
addPreferencesFromResource(R.xml.preferences);
} else {
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
当我从 com.badlogic.gdx.Game
的子类调用它时,我使用
When I call it from a subclass of com.badlogic.gdx.Game
, I use
Preferences pref = Gdx.app.getPreferences("preferences");
pref.getInteger("framerate");
pref
内的键数为 0.
有人知道如何解决这个问题吗?
Anyone have a clue as to how this might be fixed?
推荐答案
感谢http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=6365#p32981 我能够解决这个问题.
Thanks to http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=6365#p32981 I was able to solve the problem.
请注意,该代码适用于 Android 2.x 和 3.0+.
Just a note, the code works for both Android 2.x and 3.0+.
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class WallpaperSettings extends PreferenceActivity {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < 11) {
addPreferencesFromResource(R.xml.preferences);
} else {
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferenceManager().setSharedPreferencesName("preferences");
getPreferenceManager().setSharedPreferencesMode(0);
}
}
}
相关文章