android 基類的封裝
阿新 • • 發佈:2018-12-28
為了寫專案的額簡便性,我們需要對程式碼做一下封裝
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(Class<? extends 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; } }