Android常用的BaseActivity

充昌勋
2023-12-01

我自己一直在用的一个BaseActivity,用得挺顺手的,贴上来,记录一下,嘿嘿,如有补充请留言

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
//这个是沉浸,暂时没写
import com.jaeger.library.StatusBarUtil;

/**
 * author  ${吴心良}
 * data: 2017/2/16.
 */
public abstract class  BaseActivity extends AppCompatActivity implements View.OnClickListener{
    /** 是否全屏 */
    public boolean isFullScreen = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFullScreen(isFullScreen);
        setContentLayout();
        beforeInitView();
        initView();
        afterInitView();
    }


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

    /** 是否全屏 */
    public void setFullScreen(boolean fullScreen) {
        if (fullScreen) {
        //注释掉的部分是之前继承Activity时候设置全屏的代码
//         requestWindowFeature(Window.FEATURE_NO_TITLE);
//         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//         WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getSupportActionBar().hide();
        } else {
//            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
    }

    /** 设置布局文件 */
    public abstract void setContentLayout();

    /** 在实例化控件之前的逻辑操作 */
    public abstract void beforeInitView();

    /** 实例化控件 */
    public abstract void initView();

    /** 实例化控件之后的操作 */
    public abstract void afterInitView();

    /** onClick方法的封装 */
    public abstract void onClickEvent(View v);

    @Override
    public void onClick(View v) {
        onClickEvent(v);
    }

    /** 获得屏幕的宽度 */
    public int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        return screenWidth;
    }

    /** 获得屏幕的高度 */
    public int getScreenHeigh() {
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenHeigh = dm.heightPixels;
        return screenHeigh;
    }

    // ----------Activity跳转----------//
    protected void startActivity(Class<?> targetClass) {
        Intent intent = new Intent(this, targetClass);
        startActivity(intent);
    }

    // 带参数跳转
    protected void startActivity(Class<?> targetClass, String key,String value) {
        Intent intent = new Intent(this, targetClass);
        intent.putExtra(key,value);
        startActivity(intent);
    }

    // 带请求码跳转
    protected void startActivity(Class<?> targetClass, int requestCode) {
        Intent intent = new Intent(this, targetClass);
        startActivityForResult(intent, requestCode);
    }

    // 带参数和请求码跳转
    protected void startActivity(Class<?> targetClass, int requestCode, Bundle bundle) {
        Intent intent = new Intent(this, targetClass);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

}
 类似资料: