329 lines
9.9 KiB
Java
329 lines
9.9 KiB
Java
package com.azhon.basic.base;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.app.Activity;
|
||
import android.content.Context;
|
||
import android.content.pm.ActivityInfo;
|
||
import android.graphics.Color;
|
||
import android.graphics.ColorMatrix;
|
||
import android.graphics.ColorMatrixColorFilter;
|
||
import android.graphics.Paint;
|
||
import android.os.Build;
|
||
import android.os.Bundle;
|
||
import android.view.View;
|
||
import android.view.Window;
|
||
import android.view.WindowManager;
|
||
import android.view.inputmethod.InputMethodManager;
|
||
import android.widget.Toast;
|
||
|
||
import androidx.annotation.LayoutRes;
|
||
import androidx.annotation.Nullable;
|
||
import androidx.appcompat.app.AppCompatActivity;
|
||
import androidx.databinding.DataBindingUtil;
|
||
import androidx.databinding.ViewDataBinding;
|
||
|
||
import com.azhon.basic.utils.ActivityUtil;
|
||
import com.azhon.basic.view.LoadingDialog;
|
||
|
||
/**
|
||
* 项目名: TODO-MVVM
|
||
* 包名 com.azhon.basic.base
|
||
* 文件名: BaseNoModelActivity
|
||
* 创建时间: 2019-03-28 on 10:28
|
||
* 描述: TODO 不需要ViewModel的页面基类
|
||
*
|
||
* @author
|
||
*/
|
||
|
||
public abstract class BaseNoModelActivity<DB extends ViewDataBinding> extends AppCompatActivity {
|
||
|
||
protected DB dataBinding;
|
||
protected Context context;
|
||
private LoadingDialog loadingDialog;
|
||
public Bundle bundle;
|
||
//获取TAG的activity名称
|
||
protected final String TAG = this.getClass().getSimpleName();
|
||
//是否显示标题栏
|
||
private boolean isShowTitle = false;
|
||
//是否显示状态栏
|
||
private boolean isShowStatusBar = true;
|
||
//是否允许旋转屏幕
|
||
private boolean isAllowScreenRoate = true;
|
||
//封装Toast对象
|
||
private static Toast toast;
|
||
|
||
|
||
@SuppressLint("SourceLockedOrientationActivity")
|
||
@Override
|
||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||
Window window = getWindow();
|
||
View decor = window.getDecorView();
|
||
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||
| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
||
window.setStatusBarColor(Color.TRANSPARENT);
|
||
// if(this.getApplicationContext().getResources().getConfiguration().uiMode == 0x21){
|
||
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
|
||
// }else {
|
||
// decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
||
// }
|
||
}
|
||
// Paint paint = new Paint();
|
||
// ColorMatrix matrix = new ColorMatrix();
|
||
// matrix.setSaturation(0);
|
||
// paint.setColorFilter(new ColorMatrixColorFilter(matrix));
|
||
// getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE, paint);
|
||
|
||
super.onCreate(savedInstanceState);
|
||
context = this;
|
||
this.bundle=savedInstanceState;
|
||
|
||
ActivityUtil.getInstance().addActivity(this);
|
||
int layoutId = initLayout();
|
||
setContentView(layoutId);
|
||
// setStatusBarColor(this,Color.TRANSPARENT);
|
||
dataBinding = initDataBinding(layoutId);
|
||
initView();
|
||
initData();
|
||
}
|
||
|
||
|
||
/**
|
||
* 修改状态栏颜色,支持4.4以上版本
|
||
* @param activity
|
||
* @param colorId
|
||
*/
|
||
public static void setStatusBarColor(Activity activity, int colorId) {
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||
Window window = activity.getWindow();
|
||
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
||
window.setStatusBarColor(colorId);
|
||
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改状态栏颜色,支持4.4以上版本
|
||
* @param activity
|
||
* @param colorId
|
||
*/
|
||
public static void setStatusBarColorW(Activity activity, int colorId,boolean statusBarTextIsWhiteColor) {
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||
Window window = activity.getWindow();
|
||
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
||
window.setStatusBarColor(activity.getResources().getColor(colorId));
|
||
if(statusBarTextIsWhiteColor) {
|
||
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
|
||
}else{
|
||
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
||
}
|
||
}
|
||
}
|
||
|
||
public void hideActionBar(){
|
||
//this.getSupportActionBar().hide();
|
||
|
||
}
|
||
/**
|
||
* 初始化要加载的布局资源ID
|
||
* 此函数优先执行于onCreate()可以做window操作
|
||
*/
|
||
protected abstract int initLayout();
|
||
|
||
|
||
/**
|
||
* 初始化DataBinding
|
||
*/
|
||
protected DB initDataBinding(@LayoutRes int layoutId) {
|
||
return DataBindingUtil.setContentView(this, layoutId);
|
||
}
|
||
|
||
/**
|
||
* 初始化视图
|
||
*/
|
||
protected abstract void initView();
|
||
|
||
/**
|
||
* 初始化数据
|
||
*/
|
||
protected abstract void initData();
|
||
|
||
/**
|
||
* 显示用户等待框
|
||
*
|
||
* @param msg 提示信息
|
||
*/
|
||
protected void showDialog(String msg) {
|
||
if (loadingDialog != null && loadingDialog.isShowing()) {
|
||
loadingDialog.setLoadingMsg(msg);
|
||
} else {
|
||
loadingDialog = new LoadingDialog(context);
|
||
loadingDialog.setLoadingMsg(msg);
|
||
loadingDialog.show();
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 隐藏等待框
|
||
*/
|
||
protected void dismissDialog() {
|
||
if (loadingDialog != null && loadingDialog.isShowing()) {
|
||
loadingDialog.dismiss();
|
||
loadingDialog = null;
|
||
}
|
||
}
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
super.onDestroy();
|
||
if (dataBinding != null) {
|
||
dataBinding.unbind();
|
||
}
|
||
ActivityUtil.getInstance().removeActivity(this);
|
||
}
|
||
|
||
|
||
/**
|
||
* 设置是否显示标题栏
|
||
*
|
||
* @param showTitle true or false
|
||
*/
|
||
public void setShowTitle(boolean showTitle) {
|
||
isShowTitle = showTitle;
|
||
}
|
||
|
||
/**
|
||
* 设置是否显示状态栏
|
||
*
|
||
* @param showStatusBar true or false
|
||
*/
|
||
public void setShowStatusBar(boolean showStatusBar) {
|
||
isShowStatusBar = showStatusBar;
|
||
}
|
||
|
||
/**
|
||
* 是否允许屏幕旋转
|
||
*
|
||
* @param allowScreenRoate true or false
|
||
*/
|
||
public void setAllowScreenRoate(boolean allowScreenRoate) {
|
||
isAllowScreenRoate = allowScreenRoate;
|
||
}
|
||
|
||
/**
|
||
* 保证同一按钮在1秒内只会响应一次点击事件
|
||
*/
|
||
public abstract class OnSingleClickListener implements View.OnClickListener {
|
||
//两次点击按钮之间的间隔,目前为1000ms
|
||
private static final int MIN_CLICK_DELAY_TIME = 1000;
|
||
private long lastClickTime;
|
||
|
||
public abstract void onSingleClick(View view);
|
||
|
||
@Override
|
||
public void onClick(View view) {
|
||
long curClickTime = System.currentTimeMillis();
|
||
if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
|
||
lastClickTime = curClickTime;
|
||
onSingleClick(view);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 同一按钮在短时间内可重复响应点击事件
|
||
*/
|
||
public abstract class OnMultiClickListener implements View.OnClickListener {
|
||
public abstract void onMultiClick(View view);
|
||
|
||
@Override
|
||
public void onClick(View v) {
|
||
onMultiClick(v);
|
||
}
|
||
}
|
||
|
||
// /**
|
||
// * 显示提示 toast
|
||
// *
|
||
// * @param msg 提示信息
|
||
// */
|
||
// @SuppressLint("ShowToast")
|
||
// public void showToast(String msg) {
|
||
// try {
|
||
// if (null == toast) {
|
||
// toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
|
||
// } else {
|
||
// toast.setText(msg);
|
||
// }
|
||
// runOnUiThread(new Runnable() {
|
||
// @Override
|
||
// public void run() {
|
||
// toast.show();
|
||
// }
|
||
// });
|
||
// } catch (Exception e) {
|
||
// e.printStackTrace();
|
||
// //解决在子线程中调用Toast的异常情况处理
|
||
// Looper.prepare();
|
||
// Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
|
||
// Looper.loop();
|
||
// }
|
||
// }
|
||
|
||
|
||
/**
|
||
* 隐藏软键盘
|
||
*/
|
||
public void hideSoftInput() {
|
||
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
|
||
if (getCurrentFocus() != null && null != imm) {
|
||
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 显示软键盘
|
||
*/
|
||
public void showSoftInput() {
|
||
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
|
||
if (getCurrentFocus() != null && null != imm) {
|
||
imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
|
||
}
|
||
}
|
||
|
||
|
||
protected void showToast(String message){
|
||
// try {
|
||
// SnackbarUtils.Short(dataBinding.getRoot(),message).show();
|
||
//
|
||
// }catch (Exception e){e.printStackTrace();}
|
||
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
|
||
|
||
}
|
||
|
||
protected void showToast(String message,int color){
|
||
try {
|
||
// SnackbarUtils.Short(dataBinding.getRoot(),message,color).show();
|
||
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
|
||
|
||
|
||
}catch (Exception e){e.printStackTrace();}
|
||
|
||
}
|
||
|
||
protected void showToast(View view,String message){
|
||
try {
|
||
// SnackbarUtils.Short(view,message).show();
|
||
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
|
||
|
||
}catch (Exception e){e.printStackTrace();}
|
||
|
||
|
||
}
|
||
}
|