Android:使用@IntDef代替Enum

一、引入@IntDef的jar包:

compile 'com.android.support:support-annotations:25.3.1'

二、使用:

1.使用@IntDef定义状态

/**
 * 定义状态
 */
public class NodataState {

    @IntDef({State.NO_NETWORK, State.CONNECT_ERROR, State.NO_DATA})

    @Retention(RetentionPolicy.SOURCE)
    public @interface State {
        int NO_NETWORK = 1; //当前网络不可用
        int CONNECT_ERROR = 2; //数据加载失败
        int NO_DATA = 3; //暂无数据
    }
}

2.使用定义的状态值:

public void onRefreshClick() {
        switch (mType) {
            case NodataState.State.NO_NETWORK:

                break;
            case NodataState.State.NO_DATA:

                break;
            case NodataState.State.CONNECT_ERROR:

                break;

        }
    }





猜你喜欢

转载自blog.csdn.net/a526001650a/article/details/78590031
今日推荐