Stu*_*upe 20 variables android quit
我想要保存一些变量,当我关闭我的应用程序并在打开应用程序后加载它们(用于游戏中的统计信息)
我怎样才能做到这一点?
编辑:这里我的代码:
TextView test1;
String punkte = "15";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences save = getSharedPreferences(punkte, 0);
save.edit().putString("score", punkte);
SharedPreferences load = getSharedPreferences(punkte, 0);
String points = load.getString("score", "0");
test1 = (TextView) findViewById(R.id.test1);
test1.setText(points);
}
Run Code Online (Sandbox Code Playgroud)
Mxy*_*xyk 40
你应该使用SharedPrefences.它们使用起来非常简单,并将变量存储在应用程序数据中.只要用户从未在您的应用程序的设置中点击"清除数据",他们将始终在那里.
这是一个代码示例.
要访问变量:
SharedPreferences mPrefs = getSharedPreferences("label", 0);
String mString = mPrefs.getString("tag", "default_value_if_variable_not_found");
Run Code Online (Sandbox Code Playgroud)
编辑变量并提交(存储)它们:
SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putString("tag", value_of_variable).commit();
Run Code Online (Sandbox Code Playgroud)
确保两个"标记"字段匹配!
| 归档时间: |
|
| 查看次数: |
35038 次 |
| 最近记录: |