1. 程式人生 > >封裝BaseActivity基類加沉浸式標題欄 隱藏狀態列 加DialogUtils進度條工具類

封裝BaseActivity基類加沉浸式標題欄 隱藏狀態列 加DialogUtils進度條工具類

//基類BaseActivity

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener{
    private boolean isstatus=false;//沉浸式透明狀態列標識
private boolean showActionBar=true;//是否顯示標題欄ActionBar
private boolean isFullScreen=false;//隱藏狀態列
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super
.onCreate(savedInstanceState); setContentView(bindLayout()); initViews(); initData();; setOnclick(); } //初始化佈局檔案 public abstract int bindLayout(); //初始化控制元件 public abstract void initViews(); //初始化資料 public abstract void initData(); //初始化點選事件 public abstract void
setOnclick(); //初始化跳轉 public void startActivity(Class<?> clz){ Intent intent=new Intent(this,clz); startActivity(intent); } //跳轉傳值 public void startActivity(Bundle bundle, Class<Main2Activity> clz){ Intent intent=new Intent(this,clz); intent.putExtras(bundle); startActivity(intent); } ////透明狀態列方法
public void setStatus(boolean isstatus) { this.isstatus = isstatus; if (isstatus) { //判斷當前裝置的版本號大於等於19,走這個程式碼,用於程式碼適配 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態列 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } } //隱藏顯示標題欄 public void setAction(boolean showActionBar) { this.showActionBar=showActionBar; //顯示 if(showActionBar){ getSupportActionBar().show(); }else{ //隱藏 getSupportActionBar().hide(); } } //設定是否隱藏狀態列,顯示全屏 public void setFullScreen(boolean fullScreen){ this.isFullScreen=isstatus; if(isFullScreen){ //是全屏的時候 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } } //Toast public void showToats(String msg){ Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); } }
//子類,也就是我們的MainActivity
public class MainActivity extends BaseActivity {
     Button btn1,btn2,btn3;
    //初始化佈局
@Override
public int bindLayout() {
        return R.layout.activity_main;
    }
   //初始化控制元件
@Override
public void initViews() {
           btn1=findViewById(R.id.btn1);
           btn2=findViewById(R.id.btn2);
           btn3=findViewById(R.id.btn3);
    }

    @Override
public void initData() {
         //設定狀態列沉浸式
setStatus(true);
         //是否顯示actionbar 標題欄
setAction(false);
         //是否全屏 隱藏狀態列
setFullScreen(true);
         //顯示進度,每次發起網路請求時候都有一個載入進度提示
DialogUtils.getmInstance(this).showprogressDialog("正在載入......");

    }

    @Override
public void setOnclick() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }


    @Override
public void onClick(View v) {
             switch (v.getId()){
                 case R.id.btn1:
                      startActivity(Main2Activity.class);
                      break;
                 case R.id.btn2:
                     Bundle bundle=new Bundle();
                     bundle.putString("yu","傻");
                      startActivity(bundle,Main2Activity.class);
                      break;
                 case R.id.btn3:
                     showToats("登入成功");
                     break;
             }
    }
}
//DialogUtils 進度條工具類  覺得樣式不好看自己心View
public class DialogUtils {
    private Context context;
    private static  DialogUtils mInstance;
    private ProgressDialog progressDialog;
    private DialogUtils(Context context){
        this.context=context;
    }
    //雙重檢驗鎖的單例模式
public static DialogUtils getmInstance(Context context){
             if(mInstance==null){
                 synchronized (DialogUtils.class){
                     if (mInstance==null){
                         mInstance=new DialogUtils(context);
                     }
                 }
             }
             return mInstance;
    }
    //顯示
public void showprogressDialog(String msg){
        progressDialog=new ProgressDialog(context);
        progressDialog.setCancelable(true);//是否點選外部消失
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//顯示效果
progressDialog.setMessage(msg);
        progressDialog.show();

    }
    //隱藏
public void hideprogressDialog(String msg){
        if(progressDialog!=null&&progressDialog.isShowing()){
            progressDialog.dismiss();
        }

    }
}