为了减少代码的重复率,我们常常需要使用枚举Enum,这里就来介绍一下枚举的使用。
public enum BillState {
//状态:1、待确认;3、确认;4、驳回
WAIT_CONFIRM(1, "未开票", Color.parseColor("#ff8f57"),R.drawable.kuaixiao_visit_status_going),
CONFIRM(3, "已开票",Color.parseColor("#4d4d4d"),R.drawable.kuaixiao_visit_status_finish),
TURN_DOWN(4, "已驳回",Color.parseColor("#f5715f"),R.drawable.icon_turn_down),
CANCEL(99, "已作废",Color.parseColor("#F5715F"),R.drawable.kuaixiao_visit_status_unavailable);
public int key;
public String des;
public int color;
public int resId;
BillState(int key, String des, int color,int resId) {
this.key = key;
this.des = des;
this.color = color;
this.resId = resId;
}
public static BillState getStat(int key) {
for (BillState stat : values()) {
if (stat.key == key) return stat;
}
return WAIT_CONFIRM;
}
}
枚举中设置多个常量,放上一个遍历的方法,使用时方便好用。
使用
例如;
mTradeBillInfo.status == BillState.TURN_DOWN.key