巧用enum枚举类来设置按钮等的不同状态变化


场景描述

我们的应用中常常会出现按钮等状态的变化,举个例子,当下载一个应用时,下载之前按钮的状态应为下载,并设定背景为蓝色,点击下载之后,状态变为进行中,并设定背景为红色,当下载完成了,状态变为安装,并设定背景为黄色。


相关代码

  • Status类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public enum Status {

/** 可以下载 */
STATUS_DOWNLOAD(
"下载",
R.color.blue),

/** 下载中 */
STATUS_DOWNLOAD_ING(
"下载中",
R.color.red),

/** 安装状态 */
STATUS_INSTALLED(
"安装",
R.color.yellow);

public String txt;

public int bgColor;

private GameInfoStatus(String txt, int bgColor) {
this.txt = txt;
this.bgColor = bgColor;
}

}
  • 设置