gpt4 book ai didi

java - Android 开发...应用程序结构新手

转载 作者:行者123 更新时间:2023-12-01 15:43:08 25 4
gpt4 key购买 nike

我正在实现一个 Android 应用程序。用户将选择一个称为距离的特定参数:

1- kilometers;
2- miles.

因此应用程序将保存此响应以供将来使用。

我该如何实现它?我必须创建原始资源还是必须使用 strings.xml?你能解释一下这种情况下的正确结构吗?

最佳答案

您可以使用SharedPreferences保存它。

引用:

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

To get a SharedPreferences object for your application, use one of two methods:

getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter. getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name. To write values:

Call edit() to get a SharedPreferences.Editor. Add values with methods such as putBoolean() and putString(). Commit the new values with commit() To read values, use SharedPreferences methods such as getBoolean() and getString().

下面是在计算器中保存静音按键模式首选项的示例:

public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .

// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}

@Override
protected void onStop(){
super.onStop();

// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);

// Commit the edits!
editor.commit();
}
}

关于java - Android 开发...应用程序结构新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7668657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com