Nia*_*yle 8 java android sharedpreferences android-layout
我已经能够改变活动背景的颜色(参见这篇文章).现在要求对背景图像做同样的事情.我的意思是我可以单击一个按钮,选择一个选项并将当前活动背景图像更改为新的.
这是我做的:
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();
Run Code Online (Sandbox Code Playgroud)
使用以下代码保存和加载更改:
//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();
}
Run Code Online (Sandbox Code Playgroud)
从对话框列表中选择活动背景图像时会发生更改,但下次重新启动活动时不会保存和加载更改.
我现在不知道如何解决这个问题.你能帮忙吗?非常感谢.
Ham*_*atu 12
从中选择后设置后台时,Dialog您将获得资源ID R.drawable.pix2并检索BitmapDrawable如下...
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;
Run Code Online (Sandbox Code Playgroud)
但是在onCreate()方法中,您只是传递资源ID,如下所示......
wvContent.setBackgroundColor(getSelectedItem());
Run Code Online (Sandbox Code Playgroud)
其中,getSelectedItem()返回int一个资源id 的值.
现在,在onCreate()方法中设置背景可绘制如下...
wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(getSelectedItem());
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式更新SDCard的背景信息......
String pathName = Environment.getExternalStorageDirectory().getPath() + "/folder/" + "image.jpg";
Resources res = getResources(pathName);
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable backgroundDrawable = new BitmapDrawable(res, bitmap);
wvContent.setBackgroundDrawable(backgroundDrawable);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42122 次 |
| 最近记录: |