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

时间:2017-12-08 12:17:31

标签: 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:background="@drawable/bg2"
android:id="@+id/bgplayer">

然后点击按钮

 bgplayer.setBackgroundResource(R.drawable.bg1a);

如果您想要完全退出应用并重新输入背景,那么它将是bga1

1 个答案:

答案 0 :(得分:1)

在活动中使用共享偏好设置:

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);

并将背景设置为:

bgplayer.setBackgroundResource(value);