libgdx Preferences 使用

来源:互联网 发布:iphone自制铃声mac 编辑:程序博客网 时间:2024/06/05 08:26

demo里面可以看到:

关键代码为:

Preferences prefs = Gdx.app.getPreferences(".test"); 获取配置

prefs.putBoolean("bool", true); //写入数据

prefs.flush(); // 提交数据,重要。



源码为:


package com.badlogic.gdx.tests;



import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.utils.GdxRuntimeException;


public class PreferencesTest extends GdxTest {
public void create () {
Preferences prefs = Gdx.app.getPreferences(".test");
if (prefs.contains("bool")) {
if (prefs.getBoolean("bool") != true) throw new GdxRuntimeException("bool failed");
if (prefs.getInteger("int") != 1234) throw new GdxRuntimeException("int failed");
if (prefs.getLong("long") != Long.MAX_VALUE) throw new GdxRuntimeException("long failed");
if (prefs.getFloat("float") != 1.2345f) throw new GdxRuntimeException("float failed");
if (!prefs.getString("string").equals("test!")) throw new GdxRuntimeException("string failed");
}


prefs.clear();
prefs.putBoolean("bool", true);
prefs.putInteger("int", 1234);
prefs.putLong("long", Long.MAX_VALUE);
prefs.putFloat("float", 1.2345f);
prefs.putString("string", "test!");
prefs.flush();


if (prefs.getBoolean("bool") != true) throw new GdxRuntimeException("bool failed");
if (prefs.getInteger("int") != 1234) throw new GdxRuntimeException("int failed");
if (prefs.getLong("long") != Long.MAX_VALUE) throw new GdxRuntimeException("long failed");
if (prefs.getFloat("float") != 1.2345f) throw new GdxRuntimeException("float failed");
if (!prefs.getString("string").equals("test!")) throw new GdxRuntimeException("string failed");

}


下载更多测试demo,请在:

http://download.csdn.net/detail/a332324956/7337225 下载即可。


0 0
原创粉丝点击