【发布时间】:2017-09-17 18:57:20
【问题描述】:
在 SharedPreferences 中保存 ImageButton 时遇到问题。
基本上,我有一个特殊的刷新按钮:
bbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {... }
当点击它时,它会在 HorizontalScrollView 的 LinearLayout 中创建另一个 ImageButton,背景为 drawables 文件夹中的“a”。新生成的 ImageButton 将有自己的 onClick 函数。但是,在创建它之前,该活动中没有 ImageButton,只有在单击带有几个“if 和 else if”语句的特殊 REFRESH 按钮后,它才会出现在“LinearLayoutScrollView”中,这些语句将生成具有不同可绘制背景的 ImageButtons。
我想要的是,在创建新的 ImageButton(具有来自可绘制对象的自己的背景)之后,它将保留在应用程序中,直到用户删除应用程序或 ImageButton 本身。但是,即使在重新启动应用程序后,我也希望看到保存的 ImageButton。
我现在尝试做的如下所示,但它不起作用,所以可能我做错了什么。也许有人知道我需要改变什么?
最后我正在使用 SharedPreferences 代码来保存新创建的 ImageButton,其背景为可绘制“a”。在那之后 else if 后面还有几个 else if 基本上与顶部的相同,只是对于不同的选择有不同的可绘制背景,我从另一个活动的意图中获得了作为 url 的不同选择...
根据您的建议进行了编辑(但仍然无法保存图像):
final SharedPreferences prefs = getSharedPreferences("MySavedHomeFile", MODE_PRIVATE);
final LinearLayout Row = (LinearLayout) findViewById(R.id.LinearLayoutScrollView);
bbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a = getIntent();
Intent b = getIntent();
String url1 = a.getStringExtra("url1");
String url2 = b.getStringExtra("url2");
if (url1 == null && url2 == null && url3 == null) {
Toast.makeText(getApplication(), "No new clients added!", Toast.LENGTH_SHORT).show();
} else if (url1 != null) {
int backgroundRes = prefs.getInt("savedImageButton" , R.drawable.a);
final ArrayList<String> Keys = new ArrayList<String>();
for(int ii = 0; ii < 1; ii ++){
Keys.add("Keys is : " + String.valueOf(ii));
}
LinearLayout Row = (LinearLayout) findViewById(R.id.LinearLayoutScrollView);
int width = 240;
int height = 240;
final ImageButton[] my_button = new ImageButton[Keys.size()];
for (int bt = 0; bt < Keys.size(); bt ++) {
final int Index = bt;
my_button[Index] = new ImageButton(HomePageNews.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.imageButton11);
my_button[Index].setLayoutParams(lp);
my_button[Index].setId(Index);
my_button[Index].setBackgroundResource(backgroundRes);
my_button[bt].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (my_button[Index].getId() == ((ImageButton) v).getId()) {
}
}
});
Row.addView(my_button[Index]);
}
SharedPreferences saveNewHome = getSharedPreferences("MySavedHomeFile", MODE_PRIVATE);
SharedPreferences.Editor editor = saveNewHome.edit();
editor.putInt("savedImageButton", R.drawable.a);
editor.apply();
}else if (url2 != null) {
int backgroundRes = prefs.getInt("savedImageButton" , R.drawable.b) ;
final ArrayList<String> Keys2 = new ArrayList<String>();
for(int ee = 0; ee < 1; ee ++){
Keys2.add("Keys is : " + String.valueOf(ee));
}
LinearLayout Row2 = (LinearLayout) findViewById(R.id.LinearLayoutScrollView);
int width = 240;
int height = 240;
final ImageButton[] my_button2 = new ImageButton[Keys2.size()];
for (int bt2 = 0; bt2 < Keys2.size(); bt2 ++){
final int Index2 = bt2;
my_button2[Index2] = new ImageButton(HomePageNews.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.imageButton11);
my_button2[Index2].setLayoutParams(lp);
my_button2[Index2].setId(Index2);
my_button2[Index2].setBackgroundResource(backgroundRes);
my_button2[bt2].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (my_button2[Index2].getId() == ((ImageButton) v).getId()){
}
}
});
Row2.addView(my_button2[Index2]);
}
SharedPreferences prefs = getSharedPreferences("MySavedHomeFile", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("savedImageButton", R.drawable.b);
editor.apply();
【问题讨论】:
-
您需要为该视图保存一个条件,然后在条件发生时创建它。
-
我有一个条件,这不是完整的代码,我将编辑并提交带有 if 和 else if 语句的整个按钮,以便您更好地了解。 :)
-
我不明白。您正在尝试将可绘制对象编码为字符串并将其保存在 XML 文件中?
-
您必须将位图序列化为字节数组...但实际上 SharedPrefences 不是您想要的。您可以将位图写入磁盘并以这种方式保存图像
-
我正在尝试使用不同的 if else 语句生成 ImageButtons,这意味着不同的背景(可绘制对象)取决于用户在另一个活动中的选择。我实际上应该保存生成的 ImageButton,但是我需要保留其特殊的可绘制背景,这将取决于用户在之前的活动中选择的图像 URL。我希望它有所帮助,它有点复杂......:/
标签: android sharedpreferences imagebutton layoutparams