Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在单击按钮时将按钮颜色保存到共享首选项?_Android_Sharedpreferences - Fatal编程技术网

Android 如何在单击按钮时将按钮颜色保存到共享首选项?

Android 如何在单击按钮时将按钮颜色保存到共享首选项?,android,sharedpreferences,Android,Sharedpreferences,我有多个按钮,我想改变按钮的颜色。我知道,但如何保存共享偏好中的颜色??如何从共享首选项中删除它们 请看 在进行键值对时,我会存储整数,然后将它们转换回颜色。例如,如果我使用键“button1 color”存储“1”,那么在使用getString(“button\u color”)时,将其转换为颜色,例如 1 -> red 2 -> orange 3 -> yellow 4 -> green 5 -> blue 请看 在进行键值对时,我会存储整数,然后将它们转换回

我有多个按钮,我想改变按钮的颜色。我知道,但如何保存共享偏好中的颜色??如何从共享首选项中删除它们

请看

在进行键值对时,我会存储整数,然后将它们转换回颜色。例如,如果我使用键
“button1 color”
存储
“1”
,那么在使用
getString(“button\u color”)
时,将其转换为颜色,例如

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

在进行键值对时,我会存储整数,然后将它们转换回颜色。例如,如果我使用键
“button1 color”
存储
“1”
,那么在使用
getString(“button\u color”)
时,将其转换为颜色,例如

1 -> red
2 -> orange
3 -> yellow
4 -> green
5 -> blue
  • 创建
    共享引用
    实例
  • 在您的
    活动中添加这些方法
  • 单击查看时调用方法
  • 创建
    共享引用
    实例
  • 在您的
    活动中添加这些方法
  • 单击查看时调用方法

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

    公共类首选项扩展活动{
    公共静态最终字符串PREF_FILE=“MyPrefsFileKey”;
    公共int setcolor1(){
    SharedReferences prefs=getContext().getSharedReferences(PREF_文件,MODE_PRIVATE);
    color=prefs.getInt(“color1key”,0);//0是默认值。
    返回颜色1;
    }
    公共int-getcolor1(int-color1){
    SharedReferences pref=getContext().getSharedReferences(pref_文件,MODE_PRIVATE);
    SharedReferences.Editor=pref.edit();
    编辑:putInt(“color1key”,color1);
    editor.apply();
    返回颜色1;
    }
    }
    
    现在,为了保存您的颜色,只需在每个案例下添加此选项。但是,您需要为每种类型创建更多的getter和setter

    preferences.getcolor1(getContext(),myIntValue1);
    
    记住在要保存的类中添加Preferences类的新实例,如下所示

    Preferences=newpreferences();
    
    如果你有任何问题,请告诉我

    --编辑显然我不能编辑,直到我有50的声誉。在代码中有.setBackgroundColor(myIntValue)
    myIntValue1是您试图保存的第一种颜色的值。

    我的方法是创建一个新的首选项类来存储所有颜色,然后为每个颜色创建getter和setter

    公共类首选项扩展活动{
    公共静态最终字符串PREF_FILE=“MyPrefsFileKey”;
    公共int setcolor1(){
    SharedReferences prefs=getContext().getSharedReferences(PREF_文件,MODE_PRIVATE);
    color=prefs.getInt(“color1key”,0);//0是默认值。
    返回颜色1;
    }
    公共int-getcolor1(int-color1){
    SharedReferences pref=getContext().getSharedReferences(pref_文件,MODE_PRIVATE);
    SharedReferences.Editor=pref.edit();
    编辑:putInt(“color1key”,color1);
    editor.apply();
    返回颜色1;
    }
    }
    
    现在,为了保存您的颜色,只需在每个案例下添加此选项。但是,您需要为每种类型创建更多的getter和setter

    preferences.getcolor1(getContext(),myIntValue1);
    
    记住在要保存的类中添加Preferences类的新实例,如下所示

    Preferences=newpreferences();
    
    如果你有任何问题,请告诉我

    --编辑显然我不能编辑,直到我有50的声誉。在代码中有.setBackgroundColor(myIntValue)
    myIntValue1是您试图保存的第一种颜色的值。

    onClick方法的可能重复项为您提供了您单击的视图,即
    视图v
    ,因此您可以从中获取背景色我想将该颜色保存到共享首选项中。.使用editor.putInt(color)添加该颜色int值查看我的第一条注释@SwapnilPatil中的链接onClick方法的可能副本为您提供了您单击的视图作为
    view v
    ,因此您可以从中获取背景颜色我想将该颜色保存到共享首选项中..使用编辑器添加该颜色int值。putInt(颜色)查看我的第一条注释@SwapnilPatil中的链接(preferences.getColour1(getContext(),myIntValue1);)这是什么myIntValue1???(preferences.getColour1(getContext(),myIntValue1);)这是什么myIntValue1???嘿@Magic,有没有关于这个问题的想法:嘿@Magic,有没有关于这个问题的想法:
    mSharedPreferences = getSharedPreferences("shared_pref_name", MODE_PRIVATE);
    
    
        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();
        }
    
    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();
        }
    });