欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

自己正在使用的BaesActivity仅供参考

程序员文章站 2022-06-21 20:13:27
自己现在使用的一个BaesActivity可能很垃圾,但是使用呢还是没有问题的。希望对一些新手有所帮助或者启发,欢迎指导,讨论public abstract class BaseActivity extends Activity { private Toast mToast; private long mLastBackClickTime = 0L; private long mExitInterval = 2000L; private boolean doubleB...

自己现在使用的一个BaesActivity可能很垃圾,但是使用呢还是没有问题的。希望对一些新手有所帮助或者启发,欢迎指导,讨论

public abstract class BaseActivity extends Activity  {
    private Toast mToast;
    private long mLastBackClickTime = 0L;
    private long mExitInterval = 2000L;
    private boolean doubleBack = true;
    protected boolean isActive = true;
    private FrameLayout mBodyContent;
    private TextView fanhuiBaseactivity;
    private LinearLayout baseactivityLayout;
    private TextView base_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTemplate()) {
            setContentView(getLayoutResource());
        } else {
            setContentView(R.layout.base_view);
            baseInitView();
            View view = LayoutInflater.from(this).inflate(getLayoutResource(), null, false);
            if (mBodyContent != null) {
                mBodyContent.removeAllViews();
                mBodyContent.addView(view);
            }
            if (setTitle()!= null && setTitle().length() != 0){
                base_title.setText(setTitle());
            }
            fanhuiBaseactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(fanhuis());
                    finish();
                }
            });
        }
        ButterKnife.bind(this);
        isActive = true;
    }

    private void baseInitView() {
        mBodyContent = (FrameLayout) findViewById(R.id.base_body);
        fanhuiBaseactivity = (TextView) findViewById(R.id.fanhui_baseactivity);
        baseactivityLayout = (LinearLayout) findViewById(R.id.baseactivity_layout);
        base_title = (TextView) findViewById(R.id.base_title);
    }
    //返回按钮页面跳转(需要写入,不需要删除即可)
    public abstract Intent fanhuis();
    //这是布局的bar信息(需要写入,不需要删除即可)
    public abstract String setTitle();
    //   可见的(需要写入,不需要删除即可)
    public void visible() {
        baseactivityLayout.setVisibility(View.VISIBLE);
    }

    //不可见的但是占位(需要写入,不需要删除即可)
    public void invisible() {
        baseactivityLayout.setVisibility(View.INVISIBLE);
    }
    //不可见的不占位(需要写入,不需要删除即可)
    public void gone() {
        baseactivityLayout.setVisibility(View.GONE);
    }

    //加载布局
    public abstract int getLayoutResource();

    public boolean isTemplate() {
        return true;
    }

    ;

    public void toast(String s) {
        if (isActive) {
            if (mToast == null) {
                mToast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
            } else {
                mToast.setText(s);
            }
            mToast.show();
        }
    }

    public void openActivity(Class<?> cls) {
        openActivity(cls, null);
    }

    public void openActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void openActivityForResult(Class<?> cls, int requestCode) {
        openActivityForResult(cls, null, requestCode);
    }

    public void openActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    public void setDoubleBack(boolean doubleBack) {
        this.doubleBack = doubleBack;
    }

    @Override
    public void onBackPressed() {
        if (doubleBack) {
            if ((System.currentTimeMillis() - mLastBackClickTime) > mExitInterval) {
                Toast.makeText(this, getString(R.string.app_exit_tip), Toast.LENGTH_SHORT).show();
                mLastBackClickTime = System.currentTimeMillis();
            } else {
                super.onBackPressed();
                moveTaskToBack(true);
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isActive = false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }
}

继承baseactivity

public class ShipUnloadingList extends BaseActivity {
    @Override
    public int getLayoutResource() {
        return R.layout.布局名字;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Intent fanhuis() {
        return null;
    }

    @Override
    public String setTitle() {
        return "title名字";
    }
//友情提示新手一下initview()直接掉。直接书写就行, 不需要调用。

希望对大家有所帮助,新手可以多看一看,欢迎大佬前来指导改进。

本文地址:https://blog.csdn.net/q992767879/article/details/112798982

相关标签: andorid