BaseActivity的简单封装

BaseActivity

public abstract class BaseActivity extends AppCompatActivity {

    protected Intent intent;
    protected Constant CONSTANT;
    protected boolean isTranslucent = false;
    private ActionBar actionBar;

    protected abstract int ContentView();

    protected abstract void init(Bundle savedInstanceState);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ContentView());
        ButterKnife.bind(this);
        intent = new Intent();
        CONSTANT = Constant.getConstant();
        init(savedInstanceState);
        if (isTranslucent) getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

    protected void ChangeActivity(Class<?> cls) {
        intent.setClass(this, cls);
        startActivity(intent);
    }

    protected void ChangeActivity(Class<?> cls, String str) {
        intent.setClass(this, cls);
        intent.putExtra("STRING", str);
        startActivity(intent);
    }

    protected void ChangeActivity(Class<?> cls, int i) {
        intent.setClass(this, cls);
        intent.putExtra("INT", i);
        startActivity(intent);
    }

    protected void toast(String content) {
        try {
            Snackbar.make(findViewById(R.id.layout_container), content, Snackbar.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
        }
    }

    protected void toast(int code) {
        String text = "";
        switch (code) {
            case 400:
                text = CONSTANT.ERROR_CODE_REQUEST_FAILURE_TEXT;
                break;
            case 401:
                text = CONSTANT.ERROR_CODE_REQUEST_ERROR_TEXT;
                break;
            case 402:
                text = CONSTANT.ERROR_CODE_CONTENT_EMPTY_TEXT;
                break;
            case 403:
                text = CONSTANT.ERROR_CODE_NOT_MATCH_TEXT;
                break;
            case 404:
                text = CONSTANT.ERROR_CODE_NO_DATA_TEXT;
                break;
            default:
                text = CONSTANT.ERROR_CODE_NO_SUCH_ERROR_CODE;
        }
        toast(text);
    }

    protected void initToolBar(Toolbar toolbar, boolean isCanBack) {
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(isCanBack);
    }

    protected void initToolBar(Toolbar toolbar, int title, boolean isCanBack) {
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(isCanBack);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

isTranslucent 用来判断是否开启沉浸式状态栏。试用ButterKnife注解代替繁琐的fidViewById。封装好Activity跳转,打印toast,以及初始化toolbar的基本方法。

public class DemoActivity extends BaseActivity {
    @Override
    protected int ContentView() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }
}

使用时在ContentView方法中直接返回布局id即可。然后直接在init方法进行相关的初始化操作即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值