如何获取视图的背景颜色以将其保存在SharedPreferences中

时间:2016-12-03 11:51:52

标签: android sharedpreferences

  

使用搜索栏我可以更改背景颜色但无法保存shanged   打开活动时的颜色

row

2 个答案:

答案 0 :(得分:0)

你可以这样做:

SharedPreferences sharedPref = getSharedPreferences("COLOR", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
// Only if you know it is a color
ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
int colorId = buttonColor.getColor();
editor.putInt("color_KEY", colorId  );
editor.commit();

答案 1 :(得分:0)

你可以这样做,

ColorDrawable buttonColor =(ColorDrawable)button.getBackground(); int colorId = buttonColor.getColor(); SharedPreferences sharedPref = getSharedPreferences("COLOR",MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt("color_KEY",id.toString()); editor.commit();