android 基类的封装

为了写项目的额简便性,我们需要对代码做一下封装

1:BaseActivity的封装


public  abstract class MybaseActivity extends Activity implements View.OnClickListener {

    /***封装toast对象**/
    private static Toast toast;
    /***是否显示标题栏*/
    private  boolean isshowtitle = true;
    /***是否显示状态栏栏*/
    private  boolean isshowstate = true;
    /*
       设置是否允许全屏
     */
    private boolean mAllowFullScreen = true;

    /** 是否沉浸状态栏 **/
    private boolean isSetStatusBar = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!isshowtitle){
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
          //沉浸式
        if (isSetStatusBar) {
            steepStatusBar();
        }
          //是否允许全屏
        if (mAllowFullScreen) {
            this.getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        if(isshowstate){
            getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                    WindowManager.LayoutParams. FLAG_FULLSCREEN);
        }
        setContentView(intiLayout());//设置布局
           initView();
           initData();
    }

      /*
         判断版本号,版本号小于19不好使
       */
    private void steepStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 透明状态栏
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

       protected   abstract void initData();//初始化数据

      protected  abstract  void initView();//初始化页面布局
       /**
     * 设置布局
     *
     * @return
     */
    public abstract int intiLayout();


    /**
     * 是否设置标题栏
     *
     * @return
     */
    public void setTitle(boolean ishow) {
        isshowtitle=ishow;
    }
     /*
        是否允许全屏
      */
    public void setAllowFullScreen(boolean allowFullScreen) {
        this.mAllowFullScreen = allowFullScreen;
    }
    /**
     * 设置是否显示状态栏
     * @param ishow
     */
    public void setState(boolean ishow) {
        isshowstate=ishow;
    }

    /**
     * 显示长toast
     * @param msg
     */
    public void toastLong(String msg){
        if (null == toast) {
            toast = new Toast(this);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setText(msg);
            toast.show();
        } else {
            toast.setText(msg);
            toast.show();
        }
    }

    /**
     * 显示短toast
     * @param msg
     */
    public void toastShort(String msg){
        if (null == toast) {
            toast = new Toast(this);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setText(msg);
            toast.show();
        } else {
            toast.setText(msg);
            toast.show();
        }
    }

    /*
       防止快速点击
     */
    private boolean fastClick() {
        long lastClick = 0;
        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }


       protected  abstract void widgetClick(View v);
    @Override
    public void onClick(View view) {

        if(fastClick()){
            widgetClick(view);//该方法就相当于点击事件,子类复写后,在该方法做操作,可防止快速点击
        }
    }
    /**
     *携带数据的页面跳转
     * @param clz
     * @param bundle
     */
    public void startActivity(Class clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

      /*
         无数据的跳转
       */
      public void OnIntent(Classextends Activity> clz) {
          Intent intent = new Intent(this,clz);
          startActivity(intent);
      }

    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 是否设置沉浸状态栏
     * @param isSetStatusBar
     */
    public void setSteepStatusBar(boolean isSetStatusBar) {
        this.isSetStatusBar = isSetStatusBar;
    }
}

     

你可能感兴趣的:(android 基类的封装)