title: Android封装BaseActivity
date: 2017-11-25 15:23:26
tags: Activity
categories: Android


Android封装BaseActivity

Activity在我们的日常开发中应该是最为常用的组件了,基础的使用大家肯定都是会用的,但是有的时候我们需要把它封装一下,功能大概如下:

  • 抽象出公共方法,增强代码复用的逻辑
  • 统一管理ActionBar
  • 统一管理Toast,Log,Dialog

直接上代码吧,好像没什么可说的了

//BaseActivity.javapublic abstract class BaseActivity extends AppCompatActivity implements DialogControl, BaseViewInterface {    private boolean _isVisible = false;    protected LayoutInflater mInflater;    protected ViewGroup mActionBar;    private LoadingUtils loading;    private String mActionBarTitle;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        onBeforeSetContentLayout();        if (getLayoutId() != 0) {            LinearLayout layout = new LinearLayout(this);            layout.setOrientation(LinearLayout.VERTICAL);            if (hasBackButton()) {                mActionBar = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.action_bar_layout, null);                TextView tv = (TextView) mActionBar.findViewById(R.id.action_bar_title);                tv.setText(getActionBarTitle());                mActionBar.findViewById(R.id.action_bar_back).setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View view) {                        Log.e("lin", "---lin--->  111111111111111111111111");                        finish();                    }                });                layout.addView(mActionBar);            }            View view = LayoutInflater.from(this).inflate(getLayoutId(), null);            layout.addView(view);            setContentView(layout);        }        ButterKnife.bind(this);        init(savedInstanceState);        initData();        initViews();    }    @Override    protected void onPause() {        super.onPause();    }    @Override    protected void onResume() {        super.onResume();    }    protected void onBeforeSetContentLayout() {    }    protected int getLayoutId() {        return 0;    }    protected View inflateView(int resId) {        return mInflater.inflate(resId, null);    }    protected String getActionBarTitle() {        return mActionBarTitle;    }    protected boolean hasBackButton() {        return false;    }    protected boolean hasActionBar() {        return false;    }    protected void init(Bundle savedInstanceState) {    }    @Override    public void showWaitDialog() {        if (_isVisible) {            return;        }        _isVisible = true;        loading = new LoadingUtils(this).setMessage("正在加载,请稍后...");        loading.show();    }    @Override    public void hideWaitDialog() {        if (_isVisible && loading != null) {            _isVisible = false;            loading.dismiss();        }    }}
//DialogControl.javapublic interface DialogControl {    public abstract void showWaitDialog();    public abstract void hideWaitDialog();}
//BaseViewInterface.javapublic interface BaseViewInterface {    void initViews();    void initData();}

以上便是对Activity的封装~

更多相关文章

  1. Android-常用代码-36片段
  2. 常用的 Android(安卓)Studio 快捷键
  3. Android中BroadcastReceiver(异步接收广播Intent)的使用
  4. Java,Android内存泄漏代码片段
  5. Android(安卓)View 拖动
  6. Android(安卓)本地代码中的LIKELY和UNLIKELY宏
  7. android 播放器合集打包(源码来源网络)
  8. android TextView实现自动滚动和Java中改变显示内容
  9. Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码

随机推荐

  1. Android学习系列一用按钮实现显示时间
  2. 谷歌宣布Android(安卓)Studio将取代Eclip
  3. Android(安卓)蓝牙相关开发
  4. Android(安卓)实现锚点定位
  5. Android布局整合include界面控件
  6. android kotlin webview内H5调用微信支付
  7. android sqlite支持的数据类型
  8. android GSM+CDMA基站定位--获取移动网络
  9. 简单理解View的onMeasure过程
  10. Android(安卓)使用TransitionManager来方