您可以保存您通过 setTag() 设置的颜色并通过 getTag() 检索它。
void OnClick(View v)
{
Integer color = v.getTag();
if (null == color) // not clicked yet
{
color = COLOR_NOT_TOGLED;
}
if (COLOR_NOT_TOGLED == color)
{
color = COLOR_TOGLED;
}
else
{
color = COLOR_NOT_TOGLED;
}
v.setBackGround(color);
v.setTag(color); // save
}
更新
好的,让我们修复它))
void OnClick(View v)
{
Object o = v.getTag();
Integer color = (null == o) ? COLOR_NOT_TOGLED : (Integer) o;
if (COLOR_NOT_TOGLED == color) {
color = COLOR_TOGLED;
} else {
color = COLOR_NOT_TOGLED;
}
v.setBackgroundColor(color);
v.setTag(color); // save
}