1268 lines
48 KiB
Java
1268 lines
48 KiB
Java
package com.ckkj.water.base;
|
||
|
||
import android.Manifest;
|
||
import android.animation.ObjectAnimator;
|
||
import android.animation.ValueAnimator;
|
||
import android.annotation.SuppressLint;
|
||
import android.app.Activity;
|
||
import android.app.AlertDialog;
|
||
import android.app.Dialog;
|
||
import android.content.Context;
|
||
import android.content.DialogInterface;
|
||
import android.content.Intent;
|
||
import android.content.IntentFilter;
|
||
import android.database.Cursor;
|
||
import android.graphics.drawable.Drawable;
|
||
import android.net.ConnectivityManager;
|
||
import android.net.Uri;
|
||
import android.os.Build;
|
||
import android.os.Bundle;
|
||
import android.os.Handler;
|
||
import android.provider.MediaStore;
|
||
import android.text.InputFilter;
|
||
import android.text.InputType;
|
||
import android.text.Spanned;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
import android.view.Gravity;
|
||
import android.view.KeyEvent;
|
||
import android.view.LayoutInflater;
|
||
import android.view.MotionEvent;
|
||
import android.view.View;
|
||
import android.view.Window;
|
||
import android.view.WindowManager;
|
||
import android.view.animation.LinearInterpolator;
|
||
import android.widget.EditText;
|
||
import android.widget.ImageView;
|
||
import android.widget.LinearLayout;
|
||
import android.widget.TextView;
|
||
import android.widget.Toast;
|
||
|
||
import androidx.annotation.NonNull;
|
||
import androidx.appcompat.app.AppCompatDelegate;
|
||
import androidx.core.app.ActivityCompat;
|
||
import androidx.viewbinding.ViewBinding;
|
||
|
||
import com.ckkj.water.dialog.LoadingProgressDialog;
|
||
import com.ckkj.water.utils.manager.ThemeManager;
|
||
import com.esafirm.imagepicker.features.ImagePicker;
|
||
import com.hjq.permissions.OnPermissionCallback;
|
||
import com.hjq.permissions.XXPermissions;
|
||
import com.ckkj.water.AppConfig;
|
||
import com.ckkj.water.EventMessage;
|
||
import com.ckkj.water.MyApplication;
|
||
import com.ckkj.water.R;
|
||
import com.ckkj.water.custom.view.PhotoViewActivity;
|
||
import com.ckkj.water.dialog.CenterTipDialog;
|
||
import com.ckkj.water.dialog.LoadingDialog;
|
||
import com.ckkj.water.login.LoginActivity;
|
||
import com.ckkj.water.receiver.NetworkChangeReceiver;
|
||
import com.ckkj.water.ConstantUtil;
|
||
import com.ckkj.water.utils.FileNameHelper;
|
||
import com.ckkj.water.utils.OfficePreviewUtil;
|
||
import com.ckkj.water.utils.dataUtil.SharePreUtil;
|
||
import com.ckkj.water.utils.manager.ActivityManager;
|
||
import com.ckkj.water.utils.permissions.PermissionUtils;
|
||
import com.ckkj.water.utils.show.ToastUtil;
|
||
import com.ckkj.water.utils.text.TextUtil;
|
||
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
|
||
|
||
import org.jetbrains.annotations.Nullable;
|
||
|
||
import java.io.File;
|
||
import java.lang.reflect.Method;
|
||
import java.lang.reflect.ParameterizedType;
|
||
import java.util.List;
|
||
import java.util.concurrent.atomic.AtomicBoolean;
|
||
|
||
import de.greenrobot.event.EventBus;
|
||
import de.greenrobot.event.Subscribe;
|
||
import de.greenrobot.event.ThreadMode;
|
||
import pub.devrel.easypermissions.EasyPermissions;
|
||
|
||
/**
|
||
* 所有activity基类
|
||
*/
|
||
|
||
@SuppressWarnings({"unchecked"})
|
||
public abstract class BaseActivity<T extends ViewBinding, P extends IPresenter>
|
||
extends RxAppCompatActivity implements IView {
|
||
//P层主导
|
||
protected P mPresenter;
|
||
protected Dialog loadingDialog;
|
||
private NetworkChangeReceiver receiver;
|
||
public static IView iView;
|
||
public TextView tipTextView;
|
||
protected T viewBinding;
|
||
protected BaseActivity mContext;
|
||
public static final int PICK_IMAGE_REQUEST_100 = 100;
|
||
public static final int PICK_IMAGE_REQUEST_200 = 200;
|
||
public static final int PICK_IMAGE_REQUEST_300 = 300;
|
||
public static final int PICK_IMAGE_REQUEST_400 = 400;
|
||
public static final int REQUEST_EVENT_CODE_10001 = 10001;
|
||
public static final int REQUEST_EVENT_CODE_10002 = 10002;
|
||
public static final int REQUEST_EVENT_CODE_10003 = 10003;
|
||
public static final int INPUT_TYPE_NORMAL = 0; // 默认
|
||
public static final int INPUT_TYPE_NUMBER = 1; // 数字
|
||
public static final int INPUT_TYPE_LETTER = 2; // 英文字母
|
||
public static final int INPUT_TYPE_CHINESE = 3; // 中文
|
||
public static final int INPUT_TYPE_NUMBER_LETTER = 4;//数字+字母
|
||
public InputFilter mInputFilter;
|
||
private static final AtomicBoolean isDialogShown = new AtomicBoolean(false); // 使用 AtomicBoolean 保证线程安全
|
||
private ObjectAnimator rotateAnimator;
|
||
|
||
@Override
|
||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||
applyTheme();
|
||
super.onCreate(savedInstanceState);
|
||
try {
|
||
EventBus.getDefault().register(this);
|
||
mContext = this;
|
||
ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
|
||
Class cls = (Class) type.getActualTypeArguments()[0];
|
||
Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class);
|
||
viewBinding = (T) inflate.invoke(null, getLayoutInflater());
|
||
setContentView(viewBinding.getRoot());
|
||
//在界面初始化之前初始化窗口
|
||
initWindows();
|
||
int layoutResID = getLayoutResId(savedInstanceState);
|
||
if (layoutResID != 0) {
|
||
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
|
||
// setContentView(layoutResID);
|
||
|
||
}
|
||
//创建P对象
|
||
mPresenter = createPresenter();
|
||
if (mPresenter != null) {
|
||
//绑定View
|
||
mPresenter.attachView(this);
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
initWidget();
|
||
initData(savedInstanceState);
|
||
}
|
||
|
||
/**
|
||
* 所有Activity主题一致
|
||
*/
|
||
protected void applyTheme() {
|
||
int nightMode = ThemeManager.getCurrentTheme(this);
|
||
AppCompatDelegate.setDefaultNightMode(nightMode);
|
||
}
|
||
|
||
/**
|
||
* 初始化窗口
|
||
*/
|
||
protected void initWindows() {
|
||
//StatusBarUtils.setWindowStatusBarColor(this, R.color.color_blue01);
|
||
// StatusBarUtil.setAndroidNativeLightStatusBar(this, true);
|
||
}
|
||
|
||
protected void initWidget() {
|
||
ActivityManager.addActivity(this, this.getClass().getName());
|
||
iView = this;
|
||
if (receiver == null) {
|
||
receiver = new NetworkChangeReceiver();
|
||
}
|
||
IntentFilter filter = new IntentFilter();
|
||
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||
registerReceiver(receiver, filter);
|
||
}
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
//把所有的数据销毁掉
|
||
if (mPresenter != null) {
|
||
mPresenter.onDestroy();//释放资源
|
||
}
|
||
this.mPresenter = null;
|
||
unregisterReceiver(receiver);
|
||
hideDialogLoading();
|
||
|
||
// 重置弹窗状态,让其他Activity可以显示弹窗
|
||
isDialogShown.set(false);
|
||
|
||
// EventBus.getDefault().unregister(this);
|
||
super.onDestroy();
|
||
}
|
||
|
||
@Subscribe(threadMode = ThreadMode.MainThread)
|
||
public void onGetMessage(EventMessage message) {
|
||
Log.d("DINGDING", "BaseActivity收到EventBus消息: " + message.msg);
|
||
|
||
if (message.msg.equals(EventMessage.USER_LOGIN_LOST)) {
|
||
Log.d("DINGDING","Activity接收一次");
|
||
Log.d("DINGDING", "触发堆栈:", new Throwable("401触发堆栈追踪"));
|
||
Log.d("DINGDING", "当前Activity状态: isFinishing=" + isFinishing() + ", isDestroyed=" + isDestroyed());
|
||
|
||
// 检查Activity状态,如果正在销毁则不处理
|
||
if (isFinishing() || isDestroyed()) {
|
||
Log.d("DINGDING", "Activity正在销毁,跳过处理");
|
||
return;
|
||
}
|
||
|
||
// 新增:检查是否为当前前台Activity
|
||
if (!isCurrentForegroundActivity()) {
|
||
Log.d("DINGDING", "不是当前前台Activity,跳过处理");
|
||
return;
|
||
}
|
||
|
||
if (isDialogShown.compareAndSet(false, true)) {
|
||
Log.d("DINGDING","Activity触发弹窗");
|
||
AppConfig.USER_LOGIN = false;
|
||
AppConfig.MEMBER_ID = 0;
|
||
AppConfig.USER_TOKEN = "";
|
||
AppConfig.USER_ID = "";
|
||
SharePreUtil.putBoolean(ConstantUtil.SHARE_PRE_NAME, mContext, ConstantUtil.USER_LOGIN, AppConfig.USER_LOGIN);
|
||
|
||
// 确保在主线程中执行
|
||
runOnUiThread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
// 再次检查Activity状态
|
||
if (isFinishing() || isDestroyed()) {
|
||
Log.d("DINGDING", "Activity已销毁,取消弹窗");
|
||
isDialogShown.set(false);
|
||
return;
|
||
}
|
||
|
||
showDialogtextInterBackKey(null, false, false, false,
|
||
"提示",
|
||
"检测到您的账号在异地登录。若非本人操作,请立即修改密码并检查账号安全!",
|
||
"我知道了", "", // 交换参数位置
|
||
new BaseFragment.OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
Log.d("DINGDING", "用户点击取消");
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
Log.d("DINGDING", "用户点击确定");
|
||
ActivityManager.removeAllActivityExceptMain();
|
||
goTo(mContext, LoginActivity.class, null);
|
||
isDialogShown.set(false);
|
||
}
|
||
}, true);
|
||
Log.d("DINGDING", "弹窗显示成功");
|
||
} catch (Exception e) {
|
||
Log.e("DINGDING", "弹窗显示失败", e);
|
||
isDialogShown.set(false);
|
||
}
|
||
}
|
||
});
|
||
} else {
|
||
Log.d("DINGDING", "弹窗已显示,跳过");
|
||
}
|
||
} else if (message.msg.equals(EventMessage.CODE_LOGIN_LOST)) {
|
||
// 检查是否满足跳转条件
|
||
if (MyApplication.canJump()) {
|
||
}
|
||
}
|
||
}
|
||
|
||
/***
|
||
* 关闭等待页面
|
||
*/
|
||
public void hideDialogLoading() {
|
||
LoadingDialog dialog = mLoadingDialog;
|
||
try {
|
||
if (dialog != null && mContext != null) {
|
||
mLoadingDialog = null;
|
||
dialog.dismiss();
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
LoadingProgressDialog progressDialog = mProgressLoadingDialog;
|
||
try {
|
||
if (progressDialog != null && mContext != null) {
|
||
mProgressLoadingDialog = null;
|
||
progressDialog.dismiss();
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
//每个activity都需要创建Presenter
|
||
public abstract P createPresenter();
|
||
|
||
//每个activity都需要创建setLayoutId
|
||
public abstract int getLayoutResId(Bundle savedInstanceState);
|
||
|
||
public void initData(Bundle savedInstanceState) {
|
||
createLoadingDialog(this, "加载中...");
|
||
// 设置输入过滤器,只允许字母和数字
|
||
mInputFilter = new InputFilter() {
|
||
@Override
|
||
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
|
||
// 正则表达式只允许字母和数字
|
||
if (source != null && !source.toString().matches("[a-zA-Z0-9]*")) {
|
||
return "";
|
||
}
|
||
return null;
|
||
}
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 模拟显示加载对话框
|
||
*/
|
||
@Override
|
||
public void showLoading() {
|
||
loadingDialog.show();
|
||
}
|
||
|
||
//模拟处理统一的数据
|
||
@Override
|
||
public void handleError(Exception e) {
|
||
e.printStackTrace();
|
||
hideLoading();
|
||
showMessage(e.getMessage());
|
||
}
|
||
|
||
@Override
|
||
public void hideLoading() {
|
||
loadingDialog.dismiss();
|
||
|
||
}
|
||
|
||
@Override
|
||
public void showMessage(String message) {
|
||
ToastUtil.showShort(this, message);
|
||
}
|
||
|
||
//加载进度
|
||
protected void createLoadingDialog(Context context, String msg) {
|
||
LayoutInflater inflater = LayoutInflater.from(context);
|
||
@SuppressLint("InflateParams") View v = inflater.inflate(R.layout.dialog_loading, null);
|
||
|
||
LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_loading_view);
|
||
tipTextView = (TextView) v.findViewById(R.id.tipTextView);
|
||
tipTextView.setText(msg);
|
||
ImageView imgLoading = v.findViewById(R.id.img_loading);
|
||
// 初始化 ObjectAnimator
|
||
rotateAnimator = ObjectAnimator.ofFloat(imgLoading, "rotation", 0f, 360f);
|
||
rotateAnimator.setDuration(1500);
|
||
rotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
|
||
rotateAnimator.setInterpolator(new LinearInterpolator()); // 匀速旋转
|
||
rotateAnimator.start();
|
||
loadingDialog = new Dialog(context, R.style.MyDialogStyle);
|
||
loadingDialog.setCanceledOnTouchOutside(false);
|
||
loadingDialog.setContentView(layout,
|
||
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
|
||
LinearLayout.LayoutParams.MATCH_PARENT));
|
||
Window window = loadingDialog.getWindow();
|
||
if (window != null) {
|
||
WindowManager.LayoutParams lp = window.getAttributes();
|
||
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
||
window.setGravity(Gravity.CENTER);
|
||
window.setAttributes(lp);
|
||
window.setWindowAnimations(R.style.PopWindowAnimStyle);
|
||
}
|
||
loadingDialog.setCanceledOnTouchOutside(false);
|
||
}
|
||
|
||
|
||
protected LoadingDialog mLoadingDialog;
|
||
|
||
/**
|
||
* 普通加载loading
|
||
*/
|
||
public void showLoadingDialog() {
|
||
LoadingDialog dialog = mLoadingDialog;
|
||
if (dialog == null && mContext != null) {
|
||
dialog = new LoadingDialog(mContext);
|
||
// 不可触摸取消
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
// 强制取消关闭界面
|
||
dialog.setCancelable(true);
|
||
dialog.setOnCancelListener(dialog1 -> finish());
|
||
mLoadingDialog = dialog;
|
||
} else {
|
||
dialog.dismiss();
|
||
}
|
||
if (dialog != null && mContext != null) {
|
||
try {
|
||
dialog.show();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 进度条加载loading
|
||
*/
|
||
protected LoadingProgressDialog mProgressLoadingDialog;
|
||
public void showProgressLoadingDialog(int progress) {
|
||
LoadingProgressDialog dialog = mProgressLoadingDialog;
|
||
if (dialog == null && mContext != null) {
|
||
dialog = new LoadingProgressDialog(mContext);
|
||
// 不可触摸取消
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
// 强制取消关闭界面
|
||
dialog.setCancelable(true);
|
||
dialog.setOnCancelListener(dialog1 -> finish());
|
||
dialog.setProgress( progress);
|
||
mProgressLoadingDialog = dialog;
|
||
} else {
|
||
dialog.dismiss();
|
||
}
|
||
if (dialog != null && mContext != null) {
|
||
try {
|
||
dialog.show();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
@Override
|
||
public void onNetChange(int netModel) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public boolean dispatchTouchEvent(MotionEvent ev) {
|
||
switch (ev.getAction()) {
|
||
case MotionEvent.ACTION_DOWN:
|
||
View view = getCurrentFocus();
|
||
TextUtil.hideKeyboard(ev, view, this);//调用方法判断是否需要隐藏键盘
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
return super.dispatchTouchEvent(ev);
|
||
}
|
||
|
||
/**
|
||
* activity跳转
|
||
*/
|
||
protected void goTo(Context from, Class<? extends BaseActivity> to, Bundle data) {
|
||
Intent i = new Intent();
|
||
i.setClass(from, to);
|
||
if (data != null) i.putExtras(data);
|
||
from.startActivity(i);
|
||
}
|
||
|
||
/**
|
||
* activity跳转
|
||
*/
|
||
protected void goToWihoutNoBase(Context from, Class<? extends Activity> to, Bundle data) {
|
||
Intent i = new Intent();
|
||
i.setClass(from, to);
|
||
if (data != null) i.putExtras(data);
|
||
from.startActivity(i);
|
||
}
|
||
|
||
/**
|
||
* activity跳转
|
||
*/
|
||
protected void goToWihoutNoPresent(Context from, Class<? extends BaseActivityNoP> to, Bundle data) {
|
||
Intent i = new Intent();
|
||
i.setClass(from, to);
|
||
if (data != null) i.putExtras(data);
|
||
from.startActivity(i);
|
||
}
|
||
|
||
/**
|
||
* 跳转返回值
|
||
*/
|
||
protected void goToForResult(Context from, Class<? extends BaseActivity> to, int questCode,
|
||
Bundle bundle) {
|
||
Intent i = new Intent();
|
||
i.setClass(from, to);
|
||
if (bundle != null) i.putExtras(bundle);
|
||
startActivityForResult(i, questCode);
|
||
}
|
||
|
||
protected void requestPermission(String... permissions) {
|
||
if (PermissionUtils.isPermissionsGranted(this, ConstantUtil.PERMISSION_QUEST, permissions)) {
|
||
Log.d("permission", "all");
|
||
nextStep();
|
||
}
|
||
}
|
||
|
||
public void noAllowPermission() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
|
||
@NonNull int[] grantResults) {
|
||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||
switch (requestCode) {
|
||
case ConstantUtil.PERMISSION_QUEST:
|
||
if (PermissionUtils.verifyPermissions(grantResults)) {
|
||
nextStep();
|
||
} else {
|
||
noAllowPermission();
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
public interface OnDialogClick {
|
||
void onCancelClick();
|
||
|
||
void onConfirmClisk();
|
||
|
||
void onConfirmClisk(String content);
|
||
|
||
void onView1Click();
|
||
}
|
||
|
||
/**
|
||
* +
|
||
*
|
||
* @param title 标题
|
||
* @param hint 提示内容2
|
||
* @param content 内容
|
||
* @param touchOutsideCancel 弹窗类型
|
||
* @param cancelText 取消 文案
|
||
* @param confirmText 确定 文案
|
||
* @param click
|
||
*/
|
||
public void showEditDialog(String title,
|
||
String hint,
|
||
String content,
|
||
boolean touchOutsideCancel,
|
||
String cancelText,
|
||
String confirmText,
|
||
int inputType,
|
||
final OnDialogClick click) {
|
||
View view = getLayoutInflater().inflate(R.layout.public_dialog_input_text, null);
|
||
EditText editText = view.findViewById(R.id.et_input);
|
||
// EditText自动获取焦点
|
||
editText.requestFocus();
|
||
//设置输入类型
|
||
setEditInputType(editText,inputType);
|
||
editText.setHint(hint);
|
||
|
||
if(!TextUtils.isEmpty(content)){
|
||
editText.setText(content);
|
||
}
|
||
if (!TextUtils.isEmpty(content)) {
|
||
editText.setSelection(editText.getText().length());
|
||
}
|
||
setTextView(R.id.tv_title, title, view);
|
||
if (!TextUtils.isEmpty(confirmText) && !TextUtils.isEmpty(cancelText)) {
|
||
((TextView) view.findViewById(R.id.btnLeft)).setText(cancelText);
|
||
((TextView) view.findViewById(R.id.btnRight)).setText(confirmText);
|
||
}
|
||
|
||
view.findViewById(R.id.btnLeft).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onCancelClick();
|
||
}
|
||
});
|
||
view.findViewById(R.id.btnRight).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (TextUtils.isEmpty(editText.getText().toString().trim())) {
|
||
ToastUtil.showShort(mContext, hint);
|
||
return;
|
||
}
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onConfirmClisk(editText.getText().toString().trim());
|
||
}
|
||
});
|
||
AlertDialog.Builder builder = null;//new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
|
||
} else {
|
||
builder = new AlertDialog.Builder(this);
|
||
}
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
dialog = builder.show();
|
||
dialog.setCanceledOnTouchOutside(touchOutsideCancel);
|
||
try {
|
||
((View) dialog.getWindow().getDecorView().findViewById(R.id.contentDialog).getParent().getParent()).setBackgroundColor(getResources().getColor(R.color.transparent));
|
||
} catch (Exception e) {
|
||
}
|
||
}
|
||
|
||
public void showPermissionDialog(Drawable imgRes, boolean isIconShow, String title,
|
||
String content) {
|
||
View view = getLayoutInflater().inflate(R.layout.permisson_top_dialog, null);
|
||
ImageView imgIcon = view.findViewById(R.id.img_permission_icon);
|
||
TextView tvTitle = view.findViewById(R.id.tv_permission_title);
|
||
TextView tvContent = view.findViewById(R.id.tv_permission_content);
|
||
tvTitle.setText(title);
|
||
tvContent.setText(content);
|
||
if (isIconShow) {
|
||
imgIcon.setVisibility(View.VISIBLE);
|
||
if (imgRes != null) {
|
||
imgIcon.setImageDrawable(imgRes);
|
||
}
|
||
} else {
|
||
imgIcon.setVisibility(View.GONE);
|
||
}
|
||
AlertDialog.Builder builder = null;//new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
|
||
} else {
|
||
builder = new AlertDialog.Builder(mContext);
|
||
}
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
dialog = builder.create();
|
||
// 获取 dialog 的 Window 对象
|
||
Window window = dialog.getWindow();
|
||
if (window != null) {
|
||
// 设置 dialog 在屏幕顶部显示
|
||
WindowManager.LayoutParams params = window.getAttributes();
|
||
params.gravity = Gravity.TOP; // 设置对话框顶部显示
|
||
window.setAttributes(params);
|
||
}
|
||
// 显示 AlertDialog
|
||
dialog.show();
|
||
// dialog = builder.show();
|
||
try {
|
||
((View) dialog.getWindow().getDecorView().findViewById(R.id.contentDialog).getParent().getParent()).setBackgroundColor(getResources().getColor(R.color.transparent));
|
||
} catch (Exception e) {
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
public Dialog dialog = null;
|
||
|
||
// 弹出一个确认对话框 并可以设置取消按钮事件
|
||
public void showDialogtext(String title, String content, String okText, String cancelText, String clickText,
|
||
final OnDialogClick click, boolean touchOutsideCancel,boolean closeActivity) {
|
||
View view = getLayoutInflater().inflate(R.layout.public_dialog_confirm, null);
|
||
setTextView(R.id.tv_title, title, view);
|
||
setTextView(R.id.tv_content, content, view);
|
||
// ((EditText) view.findViewById(R.id.txtContent)).setText(cancelText);
|
||
if (!TextUtils.isEmpty(okText) && !TextUtils.isEmpty(cancelText)) {
|
||
((TextView) view.findViewById(R.id.btnLeft)).setText(cancelText);
|
||
((TextView) view.findViewById(R.id.btnRight)).setText(okText);
|
||
}
|
||
((TextView) view.findViewById(R.id.tv_click)).setText(clickText);
|
||
|
||
view.findViewById(R.id.btnLeft).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onCancelClick();
|
||
}
|
||
});
|
||
view.findViewById(R.id.btnRight).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onConfirmClisk();
|
||
}
|
||
});
|
||
view.findViewById(R.id.tv_click).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
if (click != null)
|
||
click.onView1Click();
|
||
}
|
||
});
|
||
AlertDialog.Builder builder = null;//new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
|
||
} else {
|
||
builder = new AlertDialog.Builder(this);
|
||
}
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
dialog = builder.show();
|
||
dialog.setCanceledOnTouchOutside(touchOutsideCancel);
|
||
if(closeActivity){
|
||
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
|
||
@Override
|
||
public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent event) {
|
||
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
|
||
dialog.dismiss(); // 关闭弹窗
|
||
finish(); // 关闭页面(Activity)
|
||
return true; // 返回 true 表示事件已处理
|
||
}
|
||
return false;
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
try {
|
||
((View) dialog.getWindow().getDecorView().findViewById(R.id.contentDialog).getParent().getParent()).setBackgroundColor(getResources().getColor(R.color.transparent));
|
||
} catch (Exception e) {
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
// 弹出一个确认对话框 并可以设置取消按钮事件
|
||
public void showDialogtext(Drawable imgRes, boolean isIconShow, boolean isTextShow, boolean touchOutsideCancel, String title,
|
||
String content, String cancelText, String okText,
|
||
final BaseFragment.OnDialogClick click) {
|
||
View view = getLayoutInflater().inflate(R.layout.public_dialog_index_permis, null);
|
||
ImageView imgIcon = view.findViewById(R.id.img_dialog_icon);
|
||
TextView tvOther = view.findViewById(R.id.tv_click);
|
||
TextView tvLeft = view.findViewById(R.id.btnLeft);
|
||
if (isIconShow) {
|
||
imgIcon.setVisibility(View.VISIBLE);
|
||
if (imgRes != null) {
|
||
imgIcon.setImageDrawable(imgRes);
|
||
}
|
||
} else {
|
||
imgIcon.setVisibility(View.GONE);
|
||
}
|
||
if (isTextShow) {
|
||
tvOther.setVisibility(View.VISIBLE);
|
||
} else {
|
||
tvOther.setVisibility(View.GONE);
|
||
}
|
||
|
||
|
||
setTextView(R.id.tv_title, title, view);
|
||
setTextView(R.id.tv_content, content, view);
|
||
// ((EditText) view.findViewById(R.id.txtContent)).setText(cancelText);
|
||
if (!TextUtils.isEmpty(okText) && !TextUtils.isEmpty(cancelText)) {
|
||
((TextView) view.findViewById(R.id.btnLeft)).setText(cancelText);
|
||
((TextView) view.findViewById(R.id.btnRight)).setText(okText);
|
||
}
|
||
view.findViewById(R.id.btnLeft).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onCancelClick();
|
||
}
|
||
});
|
||
view.findViewById(R.id.btnRight).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onConfirmClick();
|
||
}
|
||
});
|
||
if (!TextUtils.isEmpty(cancelText)) {
|
||
tvLeft.setVisibility(View.VISIBLE);
|
||
} else {
|
||
tvLeft.setVisibility(View.GONE);
|
||
}
|
||
AlertDialog.Builder builder = null;//new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
|
||
} else {
|
||
builder = new AlertDialog.Builder(mContext);
|
||
}
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
dialog = builder.show();
|
||
dialog.setCanceledOnTouchOutside(touchOutsideCancel);
|
||
try {
|
||
((View) dialog.getWindow().getDecorView().findViewById(R.id.contentDialog).getParent().getParent()).setBackgroundColor(getResources().getColor(R.color.transparent));
|
||
} catch (Exception e) {
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 强制登出,拦截返回键
|
||
* @param imgRes
|
||
* @param isIconShow
|
||
* @param isTextShow
|
||
* @param touchOutsideCancel
|
||
* @param title
|
||
* @param content
|
||
* @param cancelText
|
||
* @param okText
|
||
* @param click
|
||
* @param interceptBackKey
|
||
*/
|
||
// 弹出一个确认对话框,并可以设置取消按钮事件,可选是否拦截返回键
|
||
public void showDialogtextInterBackKey(Drawable imgRes, boolean isIconShow, boolean isTextShow, boolean touchOutsideCancel, String title,
|
||
String content, String cancelText, String okText,
|
||
final BaseFragment.OnDialogClick click, boolean interceptBackKey) {
|
||
View view = getLayoutInflater().inflate(R.layout.public_dialog_index_permis, null);
|
||
ImageView imgIcon = view.findViewById(R.id.img_dialog_icon);
|
||
TextView tvOther = view.findViewById(R.id.tv_click);
|
||
TextView tvLeft = view.findViewById(R.id.btnLeft);
|
||
|
||
if (isIconShow) {
|
||
imgIcon.setVisibility(View.VISIBLE);
|
||
if (imgRes != null) {
|
||
imgIcon.setImageDrawable(imgRes);
|
||
}
|
||
} else {
|
||
imgIcon.setVisibility(View.GONE);
|
||
}
|
||
|
||
tvOther.setVisibility(isTextShow ? View.VISIBLE : View.GONE);
|
||
|
||
setTextView(R.id.tv_title, title, view);
|
||
setTextView(R.id.tv_content, content, view);
|
||
|
||
if (!TextUtils.isEmpty(okText) && !TextUtils.isEmpty(cancelText)) {
|
||
((TextView) view.findViewById(R.id.btnLeft)).setText(cancelText);
|
||
((TextView) view.findViewById(R.id.btnRight)).setText(okText);
|
||
}
|
||
|
||
view.findViewById(R.id.btnLeft).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onCancelClick();
|
||
}
|
||
});
|
||
|
||
view.findViewById(R.id.btnRight).setOnClickListener(new View.OnClickListener() {
|
||
public void onClick(View v) {
|
||
if (dialog != null && dialog.isShowing())
|
||
dialog.dismiss();
|
||
if (click != null)
|
||
click.onConfirmClick();
|
||
}
|
||
});
|
||
|
||
tvLeft.setVisibility(TextUtils.isEmpty(cancelText) ? View.GONE : View.VISIBLE);
|
||
|
||
AlertDialog.Builder builder;
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||
builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
|
||
} else {
|
||
builder = new AlertDialog.Builder(mContext);
|
||
}
|
||
|
||
builder.setInverseBackgroundForced(true);
|
||
builder.setView(view);
|
||
dialog = builder.show();
|
||
dialog.setCanceledOnTouchOutside(touchOutsideCancel);
|
||
|
||
// ✅ 拦截返回键
|
||
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
|
||
@Override
|
||
public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent event) {
|
||
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
|
||
if (interceptBackKey) {
|
||
// 拦截返回键,不做任何事
|
||
return true;
|
||
} else {
|
||
// 不拦截,关闭弹窗 + 关闭页面
|
||
if (dialog != null && dialog.isShowing()) {
|
||
dialog.dismiss();
|
||
}
|
||
if (mContext instanceof Activity) {
|
||
((Activity) mContext).finish();
|
||
}
|
||
return true; // 拦截掉返回键,自己处理
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
});
|
||
|
||
try {
|
||
((View) dialog.getWindow().getDecorView()
|
||
.findViewById(R.id.contentDialog).getParent().getParent())
|
||
.setBackgroundColor(getResources().getColor(R.color.transparent));
|
||
} catch (Exception e) {
|
||
// 安全忽略异常
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 设置指定TextView的文本
|
||
*
|
||
* @param id
|
||
* @param value textView的值
|
||
*/
|
||
public void setTextView(int id, String value, View view) {
|
||
try {
|
||
if (!TextUtils.isEmpty(value)) {
|
||
value = value.replaceAll("null", "");
|
||
((TextView) view.findViewById(id)).setText(value);
|
||
} else {
|
||
((TextView) view.findViewById(id)).setText("");
|
||
((TextView) view.findViewById(id)).setVisibility(View.GONE);
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
public void nextStep() {
|
||
}
|
||
|
||
|
||
public void imagePicker(int limitCount, int requestCode) {
|
||
String[] permis = {Manifest.permission.READ_EXTERNAL_STORAGE};
|
||
if (EasyPermissions.hasPermissions(mContext, permis)) {
|
||
ImagePicker.create(mContext)
|
||
.folderMode(false)
|
||
.showCamera(false).limit(limitCount)
|
||
.start(requestCode);
|
||
} else {
|
||
showDialogtext(mContext.getResources().getDrawable(R.mipmap.img_location), false, false, false,
|
||
"相机、存储权限", "星语明灯需要申请相机、存储权限,以便您能正常使用附件上传功能。拒绝或取消授权不影响使用其他服务",
|
||
"不允许", "允许", new BaseFragment.OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
ImagePicker.create(mContext)
|
||
.folderMode(false)
|
||
.showCamera(false).limit(limitCount)
|
||
.start(requestCode);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
|
||
@SuppressLint("WrongConstant")
|
||
public void hasWritePermission(String[] permis) {
|
||
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
||
boolean hasGetPermission = false;
|
||
XXPermissions.with(this)
|
||
.permission(permis)
|
||
.request(new OnPermissionCallback() {
|
||
@Override
|
||
public void onGranted(List<String> permissions, boolean all) {
|
||
}
|
||
|
||
@Override
|
||
public void onDenied(List<String> permissions, boolean never) {
|
||
if (never) {
|
||
Toast.makeText(mContext, "被永久拒绝授权,请手动授予权限", Toast.LENGTH_SHORT).show();
|
||
// 如果是被永久拒绝就跳转到应用权限系统设置页面
|
||
XXPermissions.startPermissionActivity(mContext, permissions);
|
||
} else {
|
||
Toast.makeText(mContext, "获取权限失败", Toast.LENGTH_SHORT).show();
|
||
}
|
||
}
|
||
});
|
||
} else {
|
||
XXPermissions.with(this)
|
||
.permission(permis)
|
||
.request(new OnPermissionCallback() {
|
||
@Override
|
||
public void onGranted(List<String> permissions, boolean all) {
|
||
}
|
||
|
||
@Override
|
||
public void onDenied(List<String> permissions, boolean never) {
|
||
// 权限被拒绝,处理相应逻辑
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 预览文件
|
||
*
|
||
* @param suffix
|
||
* @param fileUrl
|
||
*/
|
||
public void previewFiles(String suffix, String fileUrl) {
|
||
if (!TextUtils.isEmpty(suffix)) {
|
||
if (suffix.equals("jpg") || suffix.equals("jpeg") || suffix.equals("png")) {
|
||
Bundle bundle = new Bundle();
|
||
bundle.putSerializable("img_url", fileUrl);
|
||
bundle.putInt("img_item", 0);
|
||
goToWihoutNoBase(mContext, PhotoViewActivity.class, bundle);
|
||
} else if (suffix.equals("doc")
|
||
|| suffix.equals("docx")) {
|
||
Bundle bundle = new Bundle();
|
||
bundle.putString("file_url", fileUrl);
|
||
// previewDocument(fileUrl);
|
||
} else if (suffix.equals("xlsx")
|
||
|| suffix.equals("xltx")
|
||
|| suffix.equals("xls")) {
|
||
Bundle bundle = new Bundle();
|
||
bundle.putString("file_url", fileUrl);
|
||
// previewDocument(fileUrl);
|
||
} else if (suffix.equals("pdf")) {
|
||
Bundle bundle = new Bundle();
|
||
bundle.putString("file_url", fileUrl);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void previewDocument(String fileUrl) {
|
||
File destinationFile = new File(mContext.getExternalFilesDir(null), FileNameHelper.getFileNameFromUrl(fileUrl));
|
||
OfficePreviewUtil.downloadFile(fileUrl, destinationFile, new OfficePreviewUtil.DownloadCallback() {
|
||
@Override
|
||
public void onSuccess(File file) {
|
||
OfficePreviewUtil.openWithWps(mContext, file);
|
||
}
|
||
|
||
@Override
|
||
public void onError(String error) {
|
||
Log.e("DownloadError", error);
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
public void hidePermissionDialog() {
|
||
if (dialog != null && dialog.isShowing()) {
|
||
dialog.dismiss();
|
||
}
|
||
}
|
||
|
||
|
||
public boolean hasRecordPermission(String permission) {
|
||
return EasyPermissions.hasPermissions(mContext, permission);
|
||
}
|
||
|
||
public boolean hasRecordPermission(String[] permission) {
|
||
return EasyPermissions.hasPermissions(mContext, permission);
|
||
}
|
||
|
||
/**
|
||
* 获取权限
|
||
*
|
||
* @param permis
|
||
* @param title
|
||
* @param content
|
||
* @param requestCode String... permis
|
||
*/
|
||
public void getPermissionRequst(String permis, String title, String content, int requestCode) {
|
||
if (EasyPermissions.hasPermissions(mContext, permis)) {
|
||
// 已有权限,不需要处理
|
||
return;
|
||
}
|
||
// 还没权限,弹自定义确认弹窗
|
||
showDialogtext(mContext.getResources().getDrawable(R.mipmap.img_location),
|
||
false, false, true,
|
||
title,
|
||
content,
|
||
"不允许", "允许",
|
||
new BaseFragment.OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
// 用户点了“不允许”,什么都不用做
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
showPermissionDialog(mContext.getResources().getDrawable(R.mipmap.img_permission_file),
|
||
true, title, content);
|
||
// 用户点了"允许",继续判断
|
||
if (ActivityCompat.shouldShowRequestPermissionRationale(mContext, permis)) {
|
||
// 用户之前拒绝过,但没勾“不再询问” ➔ 直接请求权限
|
||
EasyPermissions.requestPermissions(mContext, content, requestCode, permis);
|
||
} else {
|
||
// 可能是第一次申请,也可能是勾了“不再询问”
|
||
// 用EasyPermissions继续请求,系统自己决定弹不弹
|
||
EasyPermissions.requestPermissions(mContext, content, requestCode, permis);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 一次获取多个权限
|
||
*
|
||
* @param permis
|
||
* @param title
|
||
* @param content
|
||
* @param requestCode String... permis
|
||
*/
|
||
public void getMorePermissionRequst(String[] permis, String title, String content, int requestCode) {
|
||
if (EasyPermissions.hasPermissions(mContext, permis)) {
|
||
// 已有权限,不需要处理
|
||
return;
|
||
}
|
||
// 还没权限,弹自定义确认弹窗
|
||
showDialogtextInterBackKey(null,
|
||
false, false, true,
|
||
title,
|
||
content,
|
||
getString(R.string.button_deny),
|
||
getString(R.string.button_allow),
|
||
new BaseFragment.OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
// 用户点了“不允许”,什么都不用做
|
||
finish();
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
showPermissionDialog(mContext.getResources().getDrawable(R.mipmap.img_permission_file),
|
||
true, title, content);
|
||
boolean shouldShowRationale = false;
|
||
for (String p : permis) {
|
||
if (ActivityCompat.shouldShowRequestPermissionRationale(mContext, p)) {
|
||
shouldShowRationale = true;
|
||
break;
|
||
}
|
||
}
|
||
// 使用 EasyPermissions 请求权限(系统决定是否弹系统框)
|
||
EasyPermissions.requestPermissions(mContext, content, requestCode, permis);
|
||
}
|
||
},false);
|
||
}
|
||
|
||
|
||
|
||
|
||
protected String getPathFromUriInBase(Uri uri) {
|
||
String[] projection = {MediaStore.Images.Media.DATA};
|
||
Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
|
||
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
||
cursor.moveToFirst();
|
||
String path = cursor.getString(column_index);
|
||
cursor.close();
|
||
return path;
|
||
}
|
||
|
||
private CenterTipDialog mCenterTipDialog;
|
||
|
||
public void showCenterTipDialog(int imageRes, String title) {
|
||
CenterTipDialog dialog = mCenterTipDialog;
|
||
if (dialog == null && mContext != null) {
|
||
dialog = new CenterTipDialog(mContext, imageRes, title);
|
||
dialog.setDismissDelay(800, null);
|
||
mCenterTipDialog = dialog;
|
||
} else {
|
||
if (dialog != null) {
|
||
dialog.dismiss();
|
||
dialog = new CenterTipDialog(mContext, imageRes, title);
|
||
dialog.setDismissDelay(800, null);
|
||
mCenterTipDialog = dialog;
|
||
}
|
||
}
|
||
if (dialog != null && mContext != null) {
|
||
try {
|
||
dialog.show();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
public void hideCenterTipDialog() {
|
||
CenterTipDialog dialog = mCenterTipDialog;
|
||
if (dialog != null && mContext != null) {
|
||
mCenterTipDialog = null;
|
||
try {
|
||
dialog.dismiss();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
// 新增:检查是否为当前前台Activity
|
||
private boolean isCurrentForegroundActivity() {
|
||
// 方法1:通过ActivityManager检查
|
||
android.app.ActivityManager am = (android.app.ActivityManager) getSystemService(ACTIVITY_SERVICE);
|
||
List<android.app.ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
|
||
if (!tasks.isEmpty()) {
|
||
String topActivityName = tasks.get(0).topActivity.getClassName();
|
||
return getClass().getName().equals(topActivityName);
|
||
}
|
||
|
||
// 方法2:通过Application检查(如果有记录当前Activity)
|
||
return MyApplication.getCurrentActivity() == this;
|
||
}
|
||
|
||
private void setEditInputType(EditText editText,int type){
|
||
|
||
switch (type){
|
||
|
||
case INPUT_TYPE_NUMBER:
|
||
//纯数字键盘
|
||
editText.setInputType(
|
||
InputType.TYPE_CLASS_NUMBER
|
||
);
|
||
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
|
||
break;
|
||
|
||
case INPUT_TYPE_LETTER:
|
||
|
||
//只能输入字母
|
||
editText.setFilters(
|
||
new InputFilter[]{
|
||
(source,start,end,dest,dstart,dend)->{
|
||
|
||
if(source.toString()
|
||
.matches("[a-zA-Z]+")){
|
||
return source;
|
||
}
|
||
return "";
|
||
}
|
||
});
|
||
break;
|
||
|
||
case INPUT_TYPE_CHINESE:
|
||
|
||
//只能输入中文
|
||
editText.setFilters(
|
||
new InputFilter[]{
|
||
(source,start,end,dest,dstart,dend)->{
|
||
|
||
if(source.toString()
|
||
.matches("[\\u4e00-\\u9fa5]+")){
|
||
return source;
|
||
}
|
||
return "";
|
||
}
|
||
});
|
||
break;
|
||
|
||
case INPUT_TYPE_NUMBER_LETTER:
|
||
|
||
//数字+字母
|
||
editText.setFilters(
|
||
new InputFilter[]{
|
||
(source,start,end,dest,dstart,dend)->{
|
||
|
||
if(source.toString()
|
||
.matches("[a-zA-Z0-9]+")){
|
||
return source;
|
||
}
|
||
return "";
|
||
}
|
||
});
|
||
break;
|
||
|
||
case INPUT_TYPE_NORMAL:
|
||
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
|
||
break;
|
||
|
||
default:
|
||
editText.setInputType(
|
||
InputType.TYPE_CLASS_TEXT
|
||
);
|
||
break;
|
||
}
|
||
|
||
}
|
||
}
|