【问题标题】:How to store color value in SharedMemory?如何在 SharedMemory 中存储颜色值?
【发布时间】:2014-05-02 06:08:50
【问题描述】:

我正在编写一个简单的程序,其中涉及某些元素的颜色变化。 对于颜色更改,我必须将颜色值(从颜色选择器中获取)存储在 SharedMemory 中。我有以下颜色选择器代码:

public class MainActivity extends Activity implements OnColorChangedListener {

    private ColorPicker picker;
    private SVBar svBar;
    private OpacityBar opacityBar;
    private Button button;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        picker = (ColorPicker) findViewById(R.id.picker);
        svBar = (SVBar) findViewById(R.id.svbar);
        opacityBar = (OpacityBar) findViewById(R.id.opacitybar);
        button = (Button) findViewById(R.id.button1);
        text = (TextView) findViewById(R.id.textView1);

        picker.addSVBar(svBar);
        picker.addOpacityBar(opacityBar);
        picker.setOnColorChangedListener(this);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.v("Color",""+ picker.getColor());
                text.setTextColor(picker.getColor());
                picker.setOldCenterColor(picker.getColor());
            }
        });
    }

    @Override
    public void onColorChanged(int color) {
        //gives the color when it's changed.
    }
}

如何将 picker.getColor() 的值存储在 SharedMemory 中?

【问题讨论】:

  • 你必须设置文本颜色。从共享首选项中获取 int 值后,您只需要使用 yourTextView.setTextColor(color);
  • 没有从这里得到任何东西?
  • 我试图理解这一点,但我不明白。 :(
  • 您必须使用共享首选项来存储它。
  • 是的..但是我没有得到代码...我正在尝试 Utils.readPreferences(MainActivity.this, "colorKey", 0) 但是如何使用它来更改值?

标签: android


【解决方案1】:
@Override 
public void onColorChanged(int color) {
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt(getString(R.string.mycolor), color);
    editor.commit();
}

要从共享首选项中获取存储的颜色,请使用此

  SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
  int storedColor = sharedPref.getInt(getString(R.string.mycolor), 0);

【讨论】:

    【解决方案2】:

    用户 SharedPreference 来存储您的颜色值。

    共享首选项是您可以存储数据的地方,这些数据会一直持续到应用程序存在于设备上,并且如果用户没有通过设置清除应用程序内存。

    如果你想存储颜色数组,参考这篇文章: https://stackoverflow.com/a/12350878/2035885

    如果只需要存储单个值,请参考以下内容:

    public static int getColor(Context context)
    {
    SharedPreferences sharedPreference = context.getSharedPreferences("your_shared_preference_name",Context.MODE_PRIVATE);
    return sharedPreference.getInt("key_to_use_to_set_and_retrieve_value", 0);
    }
    
    
    public static void setColor(Context context,int color)
    {
    SharedPreferences store = context.getSharedPreferences(MenuActivity.class.getSimpleName(),Context.MODE_PRIVATE);
    SharedPreferences.Editor edit = store.edit();
    edit.putInt("key_to_use_to_set_and_retrieve_value", color);
    edit.commit();
    }
    

    【讨论】:

      【解决方案3】:

      将其存储为int。已经是这样了。

      @Override 
      public void onColorChanged(int color) {
          // omitted: get a SharedPreferences object first
          sharedPreferences.edit().putInt("color", color).apply();
      }
      

      【讨论】:

        【解决方案4】:
        // try this way,hope this will help you...
        
        //Set To SharedPreferences
          SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
          SharedPreferences.Editor editor = sharedPreferences.edit();
          editor.putInt("pickerColor",picker.getColor() );
          editor.commit();
        
        //Get From SharedPreferences
          SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
          picker.setOldCenterColor(sharedPreferences.getInt("pickerColor",0));
        

        【讨论】:

          【解决方案5】:

          java被关注

          package com.example.sharedpreferenceexample;
          
          import android.app.Activity;
          import android.content.Context;
          import android.content.SharedPreferences;
          import android.content.SharedPreferences.Editor;
          import android.preference.PreferenceManager;
          
          public class Utils {
          
              public static void savePreferences(Activity activity, String key1,
                      int value1) {
                  SharedPreferences sp = PreferenceManager
                          .getDefaultSharedPreferences(activity.getApplicationContext());
                  Editor editor = sp.edit();
          
                  editor.putInt(key1, value1);
          
                  editor.commit();
              }
          
              public static int readPreferences(Activity activity, String key,
                      int defaultValue) {
                  SharedPreferences sp = PreferenceManager
                          .getDefaultSharedPreferences(activity.getApplicationContext());
                  return sp.getInt(key, defaultValue);
              }
          }
          

          按如下方式使用

          保存

          Utils.savePreferences(MainActivity.this, "colorKey", colorValue);
          

          阅读

           Utils.readPreferences(MainActivity.this, "colorKey", 0)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-11-17
            • 1970-01-01
            • 1970-01-01
            • 2014-10-01
            • 2011-10-04
            • 2021-09-22
            • 2010-10-21
            相关资源
            最近更新 更多