我想要的。 我目前在为我的应用设置背景时遇到问题。我想要的是一个页面,其中包含供应用户选择的壁纸/背景选项。一旦他们通过单击图像视图选择了所需的背景,整个应用程序应该使用图像作为他们的背景。这就是全部。
我做了什么。 我创建了一个活动,其中有2个ImageView作为可用壁纸的选项,并指定图像视图以在用户点击它们时设置背景图像。现在的问题是我不知道如何保存设置并将其应用于我项目中的所有其他活动。
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/settingscreen"
tools:context="com.example.naris.auin.SettingsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Background Selector"
android:id="@+id/textViewBackgroundSelector"
android:layout_below="@+id/switchNightMode"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewBackground1"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:src="@drawable/rateicon"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewBackground2"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textViewBackgroundSelector"
android:layout_toEndOf="@+id/textViewBackgroundSelector"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
设置活动
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final RelativeLayout Settingscreen = (RelativeLayout) findViewById(R.id.settingscreen);
ImageView ImageViewBackground1 = (ImageView) findViewById(R.id.imageViewBackground1);
ImageView ImageViewBackground2 = (ImageView) findViewById(R.id.imageViewBackground2);
ImageViewBackground1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Settingscreen.setBackgroundResource(R.drawable.faqicon);
}
});
ImageViewBackground2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Settingscreen.setBackgroundResource(R.drawable.rateicon);
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == android.view.KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(SettingsActivity.this,
MainActivity.class));
finish();
}
return false;
};
}
提前致谢。
答案 0 :(得分:0)
您应该使用偏好设置。 请参阅链接查看更多细节... https://developer.android.com/guide/topics/ui/settings.html
答案 1 :(得分:0)
public class SettingsActivity extends AppCompatActivity {
private static final String PREF_NAME = "nextage_quiz";
private static final int PRIVATE_MODE = 0;
SharedPreferences getPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
final RelativeLayout Settingscreen = (RelativeLayout) findViewById(R.id.settingscreen);
ImageView ImageViewBackground1 = (ImageView) findViewById(R.id.imageViewBackground1);
ImageView ImageViewBackground2 = (ImageView) findViewById(R.id.imageViewBackground2);
ImageViewBackground1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Settingscreen.setBackgroundResource(R.drawable.faqicon);
getPrefs.edit().putInt("id", R.drawable.faqicon).apply();
}
});
ImageViewBackground2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Settingscreen.setBackgroundResource(R.drawable.rateicon);
getPrefs.edit().putInt("id", R.drawable.faqicon).apply();
}
});
}
<强> MainActivity:强>
public class MainActivity extends AppCompatActivity {
private static final String PREF_NAME = "nextage_quiz";
private static final int PRIVATE_MODE = 0;
SharedPreferences getPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
ImageView background= (ImageView) findViewById(R.id.background);
if(getPrefs.getInt("id",0) != 0)
background.setBackgroundResource(getPrefs.getInt("id",0));
}
Upadate:使用这些变量
private static final String PREF_NAME = "nextage_quiz";
private static final int PRIVATE_MODE = 0;
SharedPreferences getPrefs;