Android之Activity基類封裝
檢視相關
一般的Activity裡都會用到很多的findViewById這個方法,而且每次都要強制型別轉換,這樣會顯得很繁瑣,如果在BaseActivity裡封裝好,就能省事:
protected <T extends View> T findView(int id) {
return (T) findViewById(id);
}
這樣只要是繼承了BaseActivity就能輕鬆使用LinearLayout llContent = findView(R.id.ll_content);,免去了諸多型別轉換的麻煩。
參考程式碼:
public abstract class BaseActivity extends FragmentActivity implements
OnClickListener {
/** 是否沉浸狀態列 **/
private boolean isSetStatusBar = true;
/** 是否允許全屏 **/
private boolean mAllowFullScreen = true;
/** 是否禁止旋轉螢幕 **/
private boolean isAllowScreenRoate = false;
/** 當前Activity渲染的檢視View **/
private View mContextView = null;
/** 是否輸出日誌資訊 **/
private boolean isDebug;
private String APP_NAME;
protected final String TAG = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isDebug = MApplication.isDebug;
APP_NAME = MApplication.APP_NAME;
$Log(TAG + "-->onCreate()");
try {
Bundle bundle = getIntent().getExtras();
initParms(bundle);
mContextView = LayoutInflater.from(this)
.inflate(bindLayout(), null);
if (mAllowFullScreen) {
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
if (isSetStatusBar) {
steepStatusBar();
}
setContentView(mContextView);
if (!isAllowScreenRoate) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
initView(mContextView);
doBusiness(this);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* [沉浸狀態列]
*/
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);
}
}
/**
* [初始化Bundle引數]
*
* @param parms
*/
public abstract void initParms(Bundle parms);
/**
* [繫結佈局]
*
* @return
*/
public abstract int bindLayout();
/**
* [重寫: 1.是否沉浸狀態列 2.是否全屏 3.是否禁止旋轉螢幕]
*/
// public abstract void setActivityPre();
/**
* [初始化控制元件]
*
* @param view
*/
public abstract void initView(final View view);
/**
* [業務操作]
*
* @param mContext
*/
public abstract void doBusiness(Context mContext);
/** View點選 **/
public abstract void widgetClick(View v);
@Override
public void onClick(View v) {
if (fastClick())
widgetClick(v);
}
/**
* [頁面跳轉]
*
* @param clz
*/
public void startActivity(Class<?> clz) {
startActivity(clz, null);
}
/**
* [攜帶資料的頁面跳轉]
*
* @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);
}
@SuppressWarnings("unchecked")
public <T extends View> T $(int resId) {
return (T) super.findViewById(resId);
}
/**
* [含有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);
}
@Override
protected void onResume() {
super.onResume();
$Log(TAG + "--->onResume()");
}
@Override
protected void onDestroy() {
super.onDestroy();
$Log(TAG + "--->onDestroy()");
}
/**
* [是否允許全屏]
*
* @param allowFullScreen
*/
public void setAllowFullScreen(boolean allowFullScreen) {
this.mAllowFullScreen = allowFullScreen;
}
/**
* [是否設定沉浸狀態列]
*
* @param allowFullScreen
*/
public void setSteepStatusBar(boolean isSetStatusBar) {
this.isSetStatusBar = isSetStatusBar;
}
/**
* [是否允許螢幕旋轉]
*
* @param isAllowScreenRoate
*/
public void setScreenRoate(boolean isAllowScreenRoate) {
this.isAllowScreenRoate = isAllowScreenRoate;
}
/**
* [日誌輸出]
*
* @param msg
*/
protected void $Log(String msg) {
if (isDebug) {
Log.d(APP_NAME, msg);
}
}
/**
* [防止快速點選]
*
* @return
*/
private boolean fastClick() {
long lastClick = 0;
if (System.currentTimeMillis() - lastClick <= 1000) {
return false;
}
lastClick = System.currentTimeMillis();
return true;
}
}
public abstract class BaseFragment extends Fragment implements OnClickListener {
private boolean isDebug;
private String APP_NAME;
protected final String TAG = this.getClass().getSimpleName();
private View mContextView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isDebug = MApplication.isDebug;
APP_NAME = MApplication.APP_NAME;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContextView = inflater.inflate(bindLayout(), container, false);
initView(mContextView);
doBusiness(getActivity());
return mContextView;
}
/**
* [繫結佈局]
*
* @return
*/
public abstract int bindLayout();
/**
* [初始化控制元件]
*
* @param view
*/
public abstract void initView(final View view);
/**
* [業務操作]
*
* @param mContext
*/
public abstract void doBusiness(Context mContext);
/** View點選 **/
public abstract void widgetClick(View v);
@Override
public void onClick(View v) {
if (fastClick())
widgetClick(v);
}
@SuppressWarnings("unchecked")
public <T extends View> T $(View view, int resId) {
return (T) view.findViewById(resId);
}
/**
* [日誌輸出]
*
* @param msg
*/
protected void $Log(String msg) {
if (isDebug) {
Log.d(APP_NAME, msg);
}
}
/**
* [防止快速點選]
*
* @return
*/
private long lastClick = 0;
private boolean fastClick() {
if (System.currentTimeMillis() - lastClick <= 1000) {
return false;
}
lastClick = System.currentTimeMillis();
return true;
}
}
相關文章
- Android基礎之Activity全解析Android
- Android知識點回顧之Activity基礎Android
- Android常用工具類的封裝Android封裝
- Android之Activity全面解析Android
- 靜態庫封裝之ComDir類封裝
- 靜態庫封裝之ComFile類封裝
- 靜態庫封裝之ComStr類封裝
- Android基礎-Activity基本使用Android
- 基於javascript的拖拽類封裝^o^JavaScript封裝
- 一次Android動畫工具類的封裝Android動畫封裝
- Android播放器基礎封裝庫PlayerBaseAndroid播放器封裝
- Android之Activity啟動流程詳解(基於api28)AndroidAPI
- python 打飛機專案 ( 基類封裝 )Python封裝
- Android四大元件之ActivityAndroid元件
- Android全面解析之Activity生命週期Android
- Android 四大元件之 ActivityAndroid元件
- 深入理解Android 之 Activity啟動流程(Android 10)Android
- 封裝Redis工具類封裝Redis
- 封裝Date工具類封裝
- Activity 的 "啟動流程"(基於 Android 9.0)Android
- Android四大元件之Activity篇Android元件
- Android之android:theme設定在Application 和 Activity的區別AndroidAPP
- 一、類的封裝性封裝
- 4、類和物件—封裝物件封裝
- 自用驗證類封裝封裝
- c#封裝DBHelper類C#封裝
- 優雅地封裝 Activity Result API,完美地替代 startActivityForResult()封裝API
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之檔案下載(二)Android架構MVP模式RxJava封裝
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之斷點下載(五)Android架構MVP模式RxJava封裝斷點
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之檔案上傳(三)Android架構MVP模式RxJava封裝
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之常見問題(四)Android架構MVP模式RxJava封裝
- Android中Retrofit的封裝使用Android封裝
- # 095 01 Android 零基礎入門 02 Java物件導向 02 Java封裝 01 封裝的實現 03 # 088 01 Android 零基礎入門 02 Java物件導向 02 Java封裝 03 封裝總結 01 封裝知識點總結AndroidJava物件封裝
- iOS之WKWebView封裝iOSWebView封裝
- 十五、類與封裝的概念封裝
- c# Lambda操作類封裝C#封裝
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之資料預處理(六)Android架構MVP模式RxJava封裝
- Android Activity 重建之狀態儲存與恢復Android