我有一个带有单击侦听器的名片视图。当我单击它时,卡片视图的颜色会更改,这意味着它已打开。但是,当我进行任何其他活动时,卡片视图的颜色就会改变。我不希望这种颜色改变。帮帮我。 卡视图代码...
private void setToggleEvent(GridLayout mainGrid) {
final CardView bulb=(CardView)mainGrid.getChildAt(0);
bulb.setCardBackgroundColor(Color.parseColor("#404040"));
bulb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bulb.getCardBackgroundColor().getDefaultColor()!=Color.parseColor("#FFD100"))
{
bulb.setCardBackgroundColor(Color.parseColor("#FFD100"));
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("BULB");
myRef.setValue("1");
}
else
{
bulb.setCardBackgroundColor(Color.parseColor("#404040"));
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("BULB");
myRef.setValue("0");
}
}
});
答案 0 :(得分:0)
尝试:
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
String color = sharedPreferences.getString("MY_KEY", "#fcfcfc");//#fcfcfc is defualt value!
bulb.setCardBackgroundColor(Color.parseColor(color));
bulb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bulb.getCardBackgroundColor().getDefaultColor() != Color.parseColor("#fcfcfc")) {
bulb.setCardBackgroundColor(Color.parseColor("#fcfcfc"));
sharedPreferences.edit().putString("MY_KEY", "#fcfcfc").apply();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("BULB");
myRef.setValue("1");
} else {
bulb.setCardBackgroundColor(Color.parseColor("#404040"));
sharedPreferences.edit().putString("MY_KEY", "#404040").apply();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("BULB");
myRef.setValue("0");
}
}
});