将 LibGDX 与 Android 首选项一起使用

Using LibGDX with Android Preferences(将 LibGDX 与 Android 首选项一起使用)
本文介绍了将 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);
        }
    }
}

这篇关于将 LibGDX 与 Android 首选项一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)