【发布时间】:2019-10-27 01:03:17
【问题描述】:
我有多个按钮,我想更改按钮的颜色。我知道,但是如何将这些颜色保存在共享偏好中?以及如何从共享偏好中删除它们?
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;
}
}
【问题讨论】:
-
stackoverflow.com/questions/8089054/… onClick 方法为您提供了您单击为
View v的视图,因此您可以从那里获取背景颜色 -
我想将该颜色保存到共享首选项..
-
使用 editor.putInt(color) 添加颜色 int 值
-
查看我第一条评论中的链接@SwapnilPatil