【发布时间】:2014-05-01 10:34:48
【问题描述】:
我已经能够更改活动背景的颜色(请参阅this post)。现在一个要求是对背景图像做同样的事情。我的意思是我可以单击一个按钮,选择一个选项并将当前的 Activity 背景图像更改为新的。
这是我所做的:
private SharedPreferences prefs;
private static final String SELECTED_ITEM = "SelectedItem";
private Editor sharedPrefEditor;
btnchangeColor = (ImageButton) findViewById(R.id.btnchangeColor);
btnchangeColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CharSequence[] items={getString(R.string.default),getString(R.string.pix1), getString(R.string.pix2))};
AlertDialog.Builder builder = new AlertDialog.Builder(
ContentView.this);
builder.setTitle((getResources().getString(R.string.color_switch)));
builder.setPositiveButton((R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
wvContent = (WebView) findViewById(R.id.wvContent);
int bg_color=0;
if(getString(R.string.default).equals(items[which]))
{
wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.default);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);
bg_color=R.drawable.default;
}
else if(getString(R.string.pix1).equals(items[which]))
{
wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.pix1);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);
bg_color=R.drawable.pix1;
}
else if(getString(R.string.pix2).equals(items[which]))
{
wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.pix2);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);
bg_color=R.drawable.pix2;
}
saveSelectedItem(bg_color);
}
});
builder.show();
使用以下代码保存和加载更改:
//OnCreate
wvContent = (WebView) findViewById(R.id.wvContent);
wvContent.setBackgroundColor(getSelectedItem());
...
private int getSelectedItem() {
if (prefs == null) {
prefs = PreferenceManager
.getDefaultSharedPreferences(this);
}
return prefs.getInt(SELECTED_ITEM, -1);
}
private void saveSelectedItem(int which) {
if (prefs == null) {
prefs = PreferenceManager
.getDefaultSharedPreferences(this);
}
sharedPrefEditor = prefs.edit();
sharedPrefEditor.putInt(SELECTED_ITEM, which);
sharedPrefEditor.commit();
}
当从 Dialog 列表中选择 Activity 背景图像时,它确实会发生变化,但是下次重新启动 Activity 时不会保存和加载更改。
我现在不知道如何解决这个问题。你能帮忙吗?非常感谢。
【问题讨论】:
-
请向我解释一下,您希望从用户端更改图像?当用户需要时,他会更改图像??我正确吗?
-
是的。这是一种阅读材料,用户可能希望更改背景以满足他们的视力需求。
-
好的,那么你需要将该图像放入 sdcard..当用户需要更改图像时,他只能更改或替换他的 sdcard 中的图像..所以我的意思是图像将是从客户端改变..你能明白我的意思吗,对不起我的英语..
-
这可能是下一步。此时,我只关注如何保存和保留背景更改,以便下次启动应用(或活动)时,背景图像仍然存在。
-
这是建议,请尝试让我知道它是否有效...在您的 saveSelectedItem(int which) 方法中,请删除以下行并在 onCreate 方法中实现请尝试... . if (prefs == null) { prefs = PreferenceManager .getDefaultSharedPreferences(this); }
标签: java android android-layout sharedpreferences