2019独角兽企业重金招聘Python工程师标准>>>
package com.*.*.activity; import android.content.Context; import android.content.Intent; import android.databinding.DataBindingUtil; import android.databinding.ViewDataBinding; import android.os.Bundle; import android.os.IBinder; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import com.czb.czbpos.dialog.LoadingDialog; import com.czb.czbpos.utils.StatusBarUtil; import com.czb.czbpos.utils.eventbus.Event; import com.czb.czbpos.utils.eventbus.EventBusUtil; import com.czb.czbpos.utils.toast.MyToast; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; /** * Created by Administrator on 2017/5/20. */ public abstract class BaseActivityextends AppCompatActivity { private LoadingDialog loadingDialog; /** * binding ,用于子类继承 */ protected T mBinding; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, getContentLayoutId()); initLoadingDialog(); initData(); if (isRegisterEventBus()) { EventBusUtil.register(this); } } /** * 返回子类的 layoutId * * @return */ protected abstract int getContentLayoutId(); public void configImmerssive(ViewGroup viewGroup) { StatusBarUtil.immersive(this); StatusBarUtil.setHeightAndPadding(this,viewGroup); } /** * 初始化数据 */ protected abstract void initData(); //初始化读取的dialog private void initLoadingDialog() { loadingDialog = new LoadingDialog(this); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { View v = getCurrentFocus(); if (isShouldHideKeyboard(v, ev)) { hideKeyboard(v.getWindowToken()); } } return super.dispatchTouchEvent(ev); } /** * 展示读取的 dialog */ public void showLoadingDialog() { showLoadingDialog(null); } /** * 展示读取的 dialog */ public void showLoadingDialog(String message) { if (loadingDialog != null) { if (!TextUtils.isEmpty(message)) { loadingDialog.setMessage(message); } loadingDialog.show(); } } /** * 隐藏读取的 dialog */ public void dismissLoadingDialog() { if (loadingDialog != null) { loadingDialog.dismiss(); } } /** * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时则不能隐藏 * 这是默认的处理, 但是如果有些需求不需要 隐藏软键盘的化可以重写 {@link BaseActivity#hideKeyboard(IBinder)} * 进行空实现即可 * * @param v * @param event * @return */ protected boolean isShouldHideKeyboard(View v, MotionEvent event) { if (v != null && (v instanceof EditText)) { int[] l = {0, 0}; v.getLocationInWindow(l); int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth(); if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) { // 点击EditText的事件,忽略它。 return false; } else { return true; } } // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点 return false; } /** * 获取InputMethodManager,隐藏软键盘 * 这是隐藏键盘的处理, 但是如果有些需求不需要隐藏软键盘可以重写该方法并空实现 * * @param token */ protected void hideKeyboard(IBinder token) { if (token != null) { InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS); } } public void showToast(String str) { MyToast.showContentToast(this, str); } public void showToastInfo(String str) { MyToast.showInfo(this, str); } public void showToastWarning(String str) { MyToast.showWarning(this, str); } public void showToastError(String str) { MyToast.showError(this, str); } public void showToastErrorLong(String str) { MyToast.showErrorLong(this, str); } public void showToastSuccess(String str) { MyToast.showSuccess(this, str); } //无参跳转 public void startActivity(Class> clazz) { Intent intent = new Intent(this, clazz); startActivity(intent); } //待遇有参数跳转 public void startActivityWithExtras(Class> clazz, Bundle extras) { Intent intent = new Intent(this, clazz); intent.putExtras(extras); startActivity(intent); } /** * 设置view是否可以点击 * * @param view * @param canClick */ protected void setClickAble(View view, boolean canClick) { view.setClickable(canClick); } // 是否注册事件分发@return true绑定EventBus事件分发,默认不绑定,子类需要绑定的话复写此方法返回true. protected boolean isRegisterEventBus() { return false; } @Subscribe(threadMode = ThreadMode.MAIN) public void onEventBusCome(Event event) { if (event != null) { receiveEvent(event); } } @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void onStickyEventBusCome(Event event) { if (event != null) { receiveStickyEvent(event); } } //接收到分发到事件 protected void receiveEvent(Event event) { } /** * 接受到分发的粘性事件 * * @param event 粘性事件 */ protected void receiveStickyEvent(Event event) { }
/** * 请求权限 * * @param permissions 请求的权限 * @param requestCode 请求权限的请求码 */ public void requestPermission(String[] permissions, int requestCode) { this.REQUEST_CODE_PERMISSION = requestCode; if (checkPermissions(permissions)) { permissionSuccess(REQUEST_CODE_PERMISSION); } else { permissionFail(requestCode); ListneedPermissions = getDeniedPermissions(permissions); ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION); } } // /** // * 检测所有的权限是否都已授权 // * // * @param permissions // * @return // */ private boolean checkPermissions(String[] permissions) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return true; } // // for (String permission : permissions) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } // // /** // * 获取权限集中需要申请权限的列表 // * // * @param permissions // * @return // */ private List getDeniedPermissions(String[] permissions) { List needRequestPermissionList = new ArrayList<>(); for (String permission : permissions) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED || ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { needRequestPermissionList.add(permission); } } return needRequestPermissionList; } // // /** // * 系统请求权限回调 // * // * @param requestCode // * @param permissions // * @param grantResults // */ // @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CODE_PERMISSION) { if (verifyPermissions(grantResults)) {//获取权限成功 permissionSuccess(REQUEST_CODE_PERMISSION); } else {//获取权限失败 permissionFail(REQUEST_CODE_PERMISSION); } } } // // /** // * 确认所有的权限是否都已授权 // * // * @param grantResults // * @return // */ private boolean verifyPermissions(int[] grantResults) { for (int grantResult : grantResults) { if (grantResult != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } // 获取权限成功 public void permissionSuccess(int requestCode) { Log.d(TAG, "获取权限成功=" + requestCode); } //权限获取失败 public void permissionFail(int requestCode) { Log.d(TAG, "获取权限失败=" + requestCode); // showMissingPermissionDialog(); }
@Override protected void onDestroy() { super.onDestroy(); if (isRegisterEventBus()) { EventBusUtil.unregister(this); } } }