【问题标题】:How to save color of button to shared preference on button click?如何在单击按钮时将按钮颜色保存到共享首选项?
【发布时间】:2019-10-27 01:03:17
【问题描述】:

我有多个按钮,我想更改按钮的颜色。我知道,但是如何将这些颜色保存在共享偏好中?以及如何从共享偏好中删除它们?

private void ShowPunch() {
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.fragment_edit_profile);

                WS = (Button)dialog.findViewById(R.id.ws);
                WE = (Button)dialog.findViewById(R.id.we);
                LS = (Button)dialog.findViewById(R.id.lts);
                LE = (Button)dialog.findViewById(R.id.lte);
                PS = (Button)dialog.findViewById(R.id.pts);
                PE = (Button)dialog.findViewById(R.id.pte);
                MRMS = (Button)dialog.findViewById(R.id.mrms);
                MRME = (Button)dialog.findViewById(R.id.mrme);

                WS.setOnClickListener(this) ;
                WE.setOnClickListener(this) ;
                LS.setOnClickListener(this) ;
                LE.setOnClickListener(this) ;
                PS.setOnClickListener(this) ;
                PE.setOnClickListener(this) ;
                MRMS.setOnClickListener(this) ;
                MRME.setOnClickListener(this) ;
                dialog.show();
            }

            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {


                switch (v.getId()) {
                    case R.id.ws:
                        Punching();
                        WS.setBackgroundColor(R.color.feedpos);
                        Toast.makeText(context, "Your Work Time Start From Now..", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.we:
                        Punchingwe();
                        Toast.makeText(context, "Your Work Time End Here..", Toast.LENGTH_SHORT).show();
                        WE.setBackgroundColor(myIntValue);

                        break;
                    case R.id.pts:
                        Punchingpts();
                        Toast.makeText(context, "Your Prayer Time Start From Now..", Toast.LENGTH_SHORT).show();
                        PS.setBackgroundColor(myIntValue);
                        break;
                    case R.id.pte:
                        Punchingpte();
                        Toast.makeText(context, "Your Prayer Time End Here..", Toast.LENGTH_SHORT).show();
                        PE.setBackgroundColor(myIntValue);
                        break;
                    case R.id.lts:
                        Punchinglts();
                        Toast.makeText(context, "Your Lunch Time Start From Now..", Toast.LENGTH_SHORT).show();
                         LS.setBackgroundColor(myIntValue);

                        break;
                    case R.id.lte:
                        Punchinglte();
                        Toast.makeText(context, "Your Lunch Time End Here..", Toast.LENGTH_SHORT).show();
                           LE.setBackgroundColor(myIntValue);

                        break;
                    case R.id.mrms:
                        Punchingmrms();
                        Toast.makeText(context, "Your MRM Time Start From Now..", Toast.LENGTH_SHORT).show();
                        MRMS.setBackgroundColor(myIntValue);
                       break;
                    case R.id.mrme:
                        Punchingmrme();
                        Toast.makeText(context, "Your MRM Time End Here..", Toast.LENGTH_SHORT).show();
                         MRME.setBackgroundColor(myIntValue);

                        break;
                    default:
                        break;
                }
            } 

【问题讨论】:

  • stackoverflow.com/questions/8089054/… onClick 方法为您提供了您单击为View v 的视图,因此您可以从那里获取背景颜色
  • 我想将该颜色保存到共享首选项..
  • 使用 editor.putInt(color) 添加颜色 int 值
  • 查看我第一条评论中的链接@SwapnilPatil

标签: android sharedpreferences


【解决方案1】:

请参阅this video

在执行键值对时,我会存储整数,然后将它们转换回颜色。例如,如果我将 "1" 与键 "button1-color" 一起存储,那么在使用 getString("button_color") 时将其转换为颜色,比如

1 -> red
2 -> orange
3 -> yellow
4 -> green
5 -> blue

【讨论】:

    【解决方案2】:
    1. 创建一个SharedPreference 实例
    mSharedPreferences = getSharedPreferences("shared_pref_name", MODE_PRIVATE);
    
    1. 将这些方法添加到您的Activity
    
        private void saveViewColor(int viewId, int color) {
            mSharedPreferences.edit().putInt(String.valueOf(viewId), color).apply();
        }
    
        private int getViewColor(int viewId) {
            return mSharedPreferences.getInt(String.valueOf(viewId), Color.parseColor("#FFFFFF"));
        }
    
        private void removeViewColor(int viewId) {
            mSharedPreferences.edit().remove(String.valueOf(viewId)).apply();
        }
    
        private void clearAll() {
            mSharedPreferences.edit().clear();
        }
    
    1. 查看点击时调用方法
    view.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
    
            int colorInt = Color.BLUE;
    
            Drawable drawable = v.getBackground();
    
            //get color from the view which being clicked
            if (drawable instanceof ColorDrawable) {
                colorInt = ((ColorDrawable) drawable).getColor();
            }
    
            int viewId = v.getId();
    
            v.setBackgroundColor(colorInt);
    
            //save color
            saveViewColor(viewId, colorInt);
    
            //get current view color
            getViewColor(viewId);
    
            //remove color for current view
            removeViewColor(viewId);
    
            //clear all SharedPreferences not only the color
            clearAll();
        }
    });
    

    【讨论】:

    【解决方案3】:

    我这样做的方法是创建一个新的 Preferences 类来存储所有颜色,然后为每个颜色创建 getter 和 setter。

    public class Preferences extends Activity {
    
    public static final String PREF_FILE = "MyPrefsFileKey";
    
    public int setColour1() {
            SharedPreferences prefs = getContext().getSharedPreferences(PREF_FILE, MODE_PRIVATE);
    
            colour = prefs.getInt("colour1Key", 0); //0 being the default value.
    
          return colour1;
        }
    
        public int getColour1(int colour1) {
    
            SharedPreferences pref = getContext().getSharedPreferences(PREF_FILE, MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
    
            editor.putInt("colour1Key", colour1);
            editor.apply();
            return colour1;
        }
    }
    

    现在要保存您的颜色,只需在每种情况下添加即可。但是,您需要为每个创建更多的 getter 和 setter。

    preferences.getColour1(getContext(), myIntValue1);
    

    记得在你保存的类中添加一个新的 Preferences 类实例,像这样

    Preferences preferences = new Preferences();
    

    如果您有任何问题,请告诉我。

    -- EDIT 显然我在获得 50 声望之前无法编辑。在您的代码中,您有 .setBackgroundColor (myIntValue) myIntValue1 是您尝试保存的第一种颜色的值。

    【讨论】:

    • (preferences.getColour1(getContext(), myIntValue1);) 这是什么 myIntValue1 ??
    猜你喜欢
    • 2021-12-26
    • 2012-11-14
    • 2019-01-20
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2019-12-18
    相关资源
    最近更新 更多