【问题标题】:How to save generated ImageButton in Shared Preferences如何在共享首选项中保存生成的 ImageButton
【发布时间】:2017-09-17 18:57:20
【问题描述】:

在 SharedPreferences 中保存 ImageButton 时遇到问题。

基本上,我有一个特殊的刷新按钮:

     bbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {... }

当点击它时,它会在 Horizo​​ntalScrollView 的 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


【解决方案1】:

不要存储整个图像。只需存储创建可绘制对象的资源

  editor.putInt("savedImageButton", R.drawable.a);

然后,把它拿出来

// move this outside of the button setOnClickListener 
final SharedPreferences prefs  = getSharedPreferences("MySavedHomeFile", MODE_PRIVATE);
final LinearLayout Row = (LinearLayout) findViewById(R.id.LinearLayoutScrollView);

当你创建按钮时……

// Getting the drawable 
int backgroundRes = prefs.getInt("savedImageButton" , R.drawable.a) ;
// setting the imagebutton 
my_button[Index].setBackgroundResource(backgroundRes);

新生成的 ImageButton 将有自己的 onClick 函数。

有多种方法可以设置一个所有按钮共享的 OnClickListener 接口。您只需要在每个按钮的事件中动态使用必要的数据


不过,不太确定您要在这里做什么。

每次点击bbutton,都会创建一个空列表、空数组和全新的按钮(好吧,一个按钮,因为Keys 中只有一个元素)

这也总是正确的。投射到 ImageButton 什么都不做。 if (my_button[Index].getId() == ((ImageButton) v).getId())

【讨论】:

  • Okey,这意味着通过这样做我存储了 ImageButton 的背景。那么也许你知道我如何在这个活动中调用这个保存的图像按钮及其背景,以便在我重新启动我的应用程序后在 Horizo​​ntalScrollView 中看到它?
  • 我不确定这意味着什么。这将只允许您为整个应用程序保存一张图像,而不是每个按钮一张。我不确定这是否是预期的功能,但我的回答只是替代您的String.valueOf(getResources().getDrawable
  • 如果您需要按钮上的不同图像,则需要获取不同的图像并修复 my_button[Index].setBackgroundResource(R.drawable.a) 以使用新值,如我所示
  • 我现在明白了,谢谢,但你知道找回它的代码吗?我认为它与存储非常相似?我把这个“int backgroundRes =”放在哪里,我把它放在带有if语句的按钮中吗?还是仅在活动视图中?
  • 首选项可以是全局变量。和得到非常相似是的stackoverflow.com/questions/3624280/…
猜你喜欢
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
相关资源
最近更新 更多