我有多个按钮,我想更改按钮的颜色。我知道,但是如何在共享首选项中保存这些颜色?以及如何从共享首选项中删除它们?
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;
}
}
答案 0 :(得分:0)
请参阅this video。
在做键值对时,我将存储整数,然后将它们转换回颜色。例如,如果我用密钥"1"
存储"button1-color"
,则在使用getString("button_color")
时将其转换为颜色,请说
1 -> red
2 -> orange
3 -> yellow
4 -> green
5 -> blue
答案 1 :(得分:0)
SharedPreference
实例 mSharedPreferences = getSharedPreferences("shared_pref_name", MODE_PRIVATE);
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();
}
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();
}
});
答案 2 :(得分:0)
我这样做的方式是创建一个新的Preferences类来存储所有颜色,然后为每种颜色创建吸气剂和吸气剂。
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();
如果您有任何麻烦,请告诉我。
-编辑显然,我只有50信誉才能编辑。在您的代码中,您有.setBackgroundColor(myIntValue) myIntValue1是您要保存的第一种颜色的值。