按钮上的 SharedPreferences

SharedPreferences on a Button

我正在尝试使用 shared preferences 检索 editText 值和按钮颜色。对于 editText,它工作得很好,但问题在于保存和加载按钮的颜色。请注意,我想通过 onWriteClick 方法保存按钮的当前颜色,并通过 onReadClick.

方法为按钮着色。

这是我写的:

Button btn ;
EditText c;
private static final String KEY = "key";
private SharedPreferences preferences;

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

c = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button);
preferences = getSharedPreferences("preferences", Context.MODE_PRIVATE);
         }

//shared preferences
    @SuppressLint("NewApi")
    public void onWriteClick() {
        Editor editor = preferences.edit();
        Editor color = preferences.edit();
        editor.putString(KEY, c.getText().toString());
      //Here is the problem 
       color.putString(KEY, btn.setBackgroundColor("#FFFFF"));

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
            editor.apply();
        } else {
            // This call is synchronous and should be done in a background
            // thread
            editor.commit();
        }
    }

    public void onReadClick() {
        String text = preferences.getString(KEY, null);
        c.setText(text);
    }
}

任何帮助请

在第 10 行你应该使用

(Button) findViewById(R.id.button);

而不是

(EditText) findViewById(R.id.button);

setBackgroundColor() 方法需要一个整数。 http://developer.android.com/reference/android/view/View.html#setBackgroundColor%28int%29

您可以使用 Color.parseColor() 方法来解析您的颜色字​​符串。 http://developer.android.com/reference/android/graphics/Color.html#parseColor%28java.lang.String%29

colorputString(KEY, btn.setBackgroundColor(Color.parseColor("#FFFFF")));

更新: 为什么要尝试使用 String 来存储颜色?只需使用整数。并使用适当的 Getter-Method。 setBackgroundColor()-方法 returns void.

color.putInt(KEY, btn.getBackgroundColor());

第二次更新: 没有 getBackgroundColor 方法。但是这个问题会对你有所帮助:Is there such a method call "getBackgroundColor"?

您正在使用 KEY 将 EditText 内容和 Button 颜色保存在相同的 SharedPreference 位置。定义两个标签:TEXT_KEY、COLOR_KEY 并使用 TEXT_KEY 保存文本,使用 COLOR_KEY

保存颜色
Button btn ;
EditText c;
private static final String TEXT_KEY = "tkey";
private static final String COLOR_KEY = "ckey";
private SharedPreferences preferences;

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

      c = (EditText) findViewById(R.id.editText1);
      btn = (Button) findViewById(R.id.button);
      preferences = getSharedPreferences("preferences",Context.MODE_PRIVATE);
}

    @SuppressLint("NewApi")
    public void onWriteClick() {

        PaintDrawable pd = (PaintDrawable) btn.getBackground();
        Editor editor = preferences.edit();

        editor.putString(TEXT_KEY, c.getText().toString()).commit();
        editor.putInt(COLOR_KEY, pd.getPaint().getColor()).commit();

    }

    public void onReadClick() {
        c.setText(preferences.getString(KEY, ""));
        btn.setBackgroundColor(preferences.getInt(COLOR_KEY, 0);
    }
}