Java 如何保存我的应用程序设置?

Java 如何保存我的应用程序设置?,java,android,absolutelayout,Java,Android,Absolutelayout,我如何在应用程序中保存背景设置,当它退出时,它仍然保存背景 作为默认应用程序,我将: <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:b

我如何在应用程序中保存背景设置,当它退出时,它仍然保存背景 作为默认应用程序,我将:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg2"
android:id="@+id/bgplayer">

如果您想完全退出应用程序并重新进入后台,则将是bga1

在活动中使用共享首选项:

SharedPreferences prefs = getSharedPreferences(
  "com.example.app", Context.MODE_PRIVATE);
然后像这样保存:

prefs.edit().putInt("your_key", R.drawable.bg1a).apply();
int value = prefs.getInt("your_key", defaultIntValue);
这样读:

prefs.edit().putInt("your_key", R.drawable.bg1a).apply();
int value = prefs.getInt("your_key", defaultIntValue);
并将背景设置为:

bgplayer.setBackgroundResource(value);

使用SharedReference保存您的选择…这是非常糟糕的编码实践。大学教师;不要使用已弃用的类。用你的努力去问你的疑问。这里有一个关于如何使用共享偏好的链接,非常感谢ạn@user90631147您需要使用上下文来获取共享首选项。我已经编辑了我的答案我无法完成完整的代码你可以做一个模板来帮助我,如果是这样的话谢谢你在哪里使用这个代码?在Activity中,Fragment?不确定我的答案是否正确,但我在onCreate中编写了它