From 932177a23468d904396255bd84160109698e7c2d Mon Sep 17 00:00:00 2001 From: LJYcoder <334930386@qq.com> Date: Fri, 20 Jul 2018 17:27:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=83=A8=E5=88=86=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=20=E8=B0=83=E6=95=B4=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=9A=84=E6=8F=90=E7=A4=BA=E8=AF=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VERSION.md | 4 + .../com/dev/base/mvp/model/UploadModel.java | 6 +- .../mvp/view/activity/UploadActivity.java | 2 + devring/build.gradle | 4 +- .../support/throwable/ThrowableHandler.java | 12 +- .../com/ljy/devring/image/GlideManager.java | 3 +- .../java/com/ljy/devring/util/ColorBar.java | 22 +-- .../java/com/ljy/devring/util/ConfigUtil.java | 98 ++++++++++ .../java/com/ljy/devring/util/FileUtil.java | 20 ++- .../com/ljy/devring/util/FontTypeUtil.java | 102 +++++++++++ .../java/com/ljy/devring/util/ImageUtil.java | 2 +- .../com/ljy/devring/util/KeyboardUtil.java | 169 ++++++++++++++++++ gradle.properties | 2 +- 13 files changed, 405 insertions(+), 41 deletions(-) create mode 100644 devring/src/main/java/com/ljy/devring/util/ConfigUtil.java create mode 100644 devring/src/main/java/com/ljy/devring/util/FontTypeUtil.java create mode 100644 devring/src/main/java/com/ljy/devring/util/KeyboardUtil.java diff --git a/VERSION.md b/VERSION.md index 7a747d3..8330660 100644 --- a/VERSION.md +++ b/VERSION.md @@ -1,4 +1,8 @@ ## 版本信息 + - v1.0.12、1.0.13 (2018/7/20) + - 新增工具类:ConfigUtil(设备配置相关工具类)、FontTypeUtil(修改应用字体工具类)、KeyboardUtil(软键盘工具类) + - 调整网络请求异常的提示信息 + - v1.0.10、1.0.11 (2018/6/6) - 图片加载模块的LoadOption新增边框颜色、边框粗细选项(目前仅适用于圆形模式) - 网络模块支持根据最新设置的config刷新管理者(DevRing.httpManager().refreshInstance();) diff --git a/app/src/main/java/com/dev/base/mvp/model/UploadModel.java b/app/src/main/java/com/dev/base/mvp/model/UploadModel.java index 16b5ebb..c177bbf 100644 --- a/app/src/main/java/com/dev/base/mvp/model/UploadModel.java +++ b/app/src/main/java/com/dev/base/mvp/model/UploadModel.java @@ -107,6 +107,10 @@ public File getUploadFile(Activity activity, int reqCode, Intent intent) { e.printStackTrace(); } } + //少部分机型cursor会为空,则采用以下方式获取路径。 + else { + filePath = photoUri.getPath(); + } break; case ImageUtil.REQ_PHOTO_CAMERA: @@ -146,7 +150,7 @@ public File getUploadFile(Activity activity, int reqCode, Intent intent) { @Override public void deleteTempFile(Activity activity) { //将临时保存的图片文件删除 - FileUtil.deleteFile(FileUtil.getDirectory(FileUtil.getExternalCacheDir(activity), "upload_image")); + FileUtil.deleteFile(FileUtil.getDirectory(FileUtil.getExternalCacheDir(activity), "upload_image"), false); } /** diff --git a/app/src/main/java/com/dev/base/mvp/view/activity/UploadActivity.java b/app/src/main/java/com/dev/base/mvp/view/activity/UploadActivity.java index 1fc7ed0..ad7ddcc 100644 --- a/app/src/main/java/com/dev/base/mvp/view/activity/UploadActivity.java +++ b/app/src/main/java/com/dev/base/mvp/view/activity/UploadActivity.java @@ -83,6 +83,8 @@ protected void initView(Bundle savedInstanceState) { //如果经过了配置变化而重建(如横竖屏切换),且tag为photo的DialogFragment不为空,则不使用新建的DialogFragment。 //不做此操作的话而使用新建的DialogFragment的话,会导致“弹出菜单栏---> 配置变化(如横竖屏切换)---> 点击菜单项触发dissmiss() ---> 空指针异常” + //java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object reference + //因为新建的DialogFragment还未通过show方法进行FragmentTransaction的add、commit操作。 if (savedInstanceState != null && getFragmentManager().findFragmentByTag("photo") != null) { mPhotoDialogFragment = (PhotoDialogFragment) getFragmentManager().findFragmentByTag("photo"); } diff --git a/devring/build.gradle b/devring/build.gradle index 41c5b62..ef32fe7 100644 --- a/devring/build.gradle +++ b/devring/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 23 - versionCode 12 - versionName "1.0.11" + versionCode 14 + versionName "1.0.13" } diff --git a/devring/src/main/java/com/ljy/devring/http/support/throwable/ThrowableHandler.java b/devring/src/main/java/com/ljy/devring/http/support/throwable/ThrowableHandler.java index 88c67c7..a4d8e84 100644 --- a/devring/src/main/java/com/ljy/devring/http/support/throwable/ThrowableHandler.java +++ b/devring/src/main/java/com/ljy/devring/http/support/throwable/ThrowableHandler.java @@ -26,17 +26,17 @@ public class ThrowableHandler { */ public static HttpThrowable handleThrowable(Throwable throwable) { if (throwable instanceof HttpException) { - return new HttpThrowable(HttpThrowable.HTTP_ERROR, "网络(协议)错误", throwable); + return new HttpThrowable(HttpThrowable.HTTP_ERROR, "网络(协议)异常", throwable); } else if (throwable instanceof JsonParseException || throwable instanceof JSONException || throwable instanceof ParseException) { - return new HttpThrowable(HttpThrowable.PARSE_ERROR, "解析错误", throwable); + return new HttpThrowable(HttpThrowable.PARSE_ERROR, "数据解析异常", throwable); } else if (throwable instanceof UnknownHostException) { - return new HttpThrowable(HttpThrowable.NO_NET_ERROR, "DNS解析错误(无网络)", throwable); + return new HttpThrowable(HttpThrowable.NO_NET_ERROR, "网络连接失败,请稍后重试", throwable); } else if (throwable instanceof SocketTimeoutException) { - return new HttpThrowable(HttpThrowable.TIME_OUT_ERROR, "连接超时错误", throwable); + return new HttpThrowable(HttpThrowable.TIME_OUT_ERROR, "连接超时", throwable); } else if (throwable instanceof ConnectException) { - return new HttpThrowable(HttpThrowable.CONNECT_ERROR, "连接错误", throwable); + return new HttpThrowable(HttpThrowable.CONNECT_ERROR, "连接异常", throwable); } else if (throwable instanceof javax.net.ssl.SSLHandshakeException) { - return new HttpThrowable(HttpThrowable.SSL_ERROR, "证书验证错误", throwable); + return new HttpThrowable(HttpThrowable.SSL_ERROR, "证书验证失败", throwable); } else { return new HttpThrowable(HttpThrowable.UNKNOWN, throwable.getMessage(), throwable); } diff --git a/devring/src/main/java/com/ljy/devring/image/GlideManager.java b/devring/src/main/java/com/ljy/devring/image/GlideManager.java index c92c8fc..ff187ea 100644 --- a/devring/src/main/java/com/ljy/devring/image/GlideManager.java +++ b/devring/src/main/java/com/ljy/devring/image/GlideManager.java @@ -13,7 +13,6 @@ import com.bumptech.glide.load.MultiTransformation; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.GlideException; -import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; @@ -235,7 +234,7 @@ private RequestBuilder load(RequestBuilder requestBuilder, LoadOption loadOption grayscaleTransformation = new GrayscaleTransformation(); } - MultiTransformation multiTransformation = getMultiTransformation(new CenterCrop(), circleTransformation, roundedCornersTransformation, blurTransformation, grayscaleTransformation); + MultiTransformation multiTransformation = getMultiTransformation(circleTransformation, roundedCornersTransformation, blurTransformation, grayscaleTransformation); if (multiTransformation != null) requestOptions.transform(multiTransformation); } return requestBuilder.apply(requestOptions); diff --git a/devring/src/main/java/com/ljy/devring/util/ColorBar.java b/devring/src/main/java/com/ljy/devring/util/ColorBar.java index 9423bb2..bc7f56f 100644 --- a/devring/src/main/java/com/ljy/devring/util/ColorBar.java +++ b/devring/src/main/java/com/ljy/devring/util/ColorBar.java @@ -3,7 +3,6 @@ import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; -import android.content.res.Resources; import android.graphics.Color; import android.os.Build; import android.support.annotation.ColorInt; @@ -318,7 +317,7 @@ private int limitDepthOrAlpha(int depthOrAlpha) { private View createStatusBarView(Context context, @ColorInt int color) { View statusBarView = new View(context); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams - (FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context)); + (FrameLayout.LayoutParams.MATCH_PARENT, ConfigUtil.getStatusBarHeight(context)); params.gravity = Gravity.TOP; statusBarView.setLayoutParams(params); statusBarView.setBackgroundColor(color); @@ -328,7 +327,7 @@ private View createStatusBarView(Context context, @ColorInt int color) { private View createNavBarView(Context context, @ColorInt int color) { View navBarView = new View(context); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams - (FrameLayout.LayoutParams.MATCH_PARENT, getNavigationHeight(context)); + (FrameLayout.LayoutParams.MATCH_PARENT, ConfigUtil.getNavigationBarHeight(context)); params.gravity = Gravity.BOTTOM; navBarView.setLayoutParams(params); navBarView.setBackgroundColor(color); @@ -385,23 +384,6 @@ private void setRootView(Activity activity, boolean fit) { } } - - - private int getStatusBarHeight(Context context) { - Resources resources = context.getResources(); - int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); - return resources.getDimensionPixelSize(resourceId); - } - - - private int getNavigationHeight(Context context) { - Resources resources = context.getResources(); - int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); - return resources.getDimensionPixelSize(resourceId); - } - - - public static class ColorBuilder { @ColorInt diff --git a/devring/src/main/java/com/ljy/devring/util/ConfigUtil.java b/devring/src/main/java/com/ljy/devring/util/ConfigUtil.java new file mode 100644 index 0000000..91b943a --- /dev/null +++ b/devring/src/main/java/com/ljy/devring/util/ConfigUtil.java @@ -0,0 +1,98 @@ +package com.ljy.devring.util; + +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.res.Resources; +import android.os.Build; +import android.util.DisplayMetrics; +import android.view.Display; +import android.view.WindowManager; + +import java.lang.reflect.Method; + +/** + * author: ljy + * date: 2018/7/20 + * description: + */ + +public class ConfigUtil { + + /** + * 获取屏幕宽高,会加上虚拟按键栏的长度 + * + * @param context + * @return + */ + public static int[] getScreenSize(Context context) { + int[] size = new int[2]; + if (Build.VERSION.SDK_INT >= 17) { + DisplayMetrics dm = new DisplayMetrics(); + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + windowManager.getDefaultDisplay().getRealMetrics(dm); + size[0] = dm.widthPixels; // 屏幕宽 + size[1] = dm.heightPixels; // 屏幕高 + } else { + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + Display display = windowManager.getDefaultDisplay(); + DisplayMetrics dm = new DisplayMetrics(); + @SuppressWarnings("rawtypes") Class c; + try { + c = Class.forName("android.view.Display"); + @SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class); + method.invoke(display, dm); + } catch (Exception e) { + e.printStackTrace(); + } + size[0] = dm.widthPixels; // 屏幕宽 + size[1] = dm.heightPixels; // 屏幕高 + } + return size; + } + + /** + * 获取屏幕的长边(即竖屏下的高,横屏下的宽) + */ + public static int getScreenLongSide(Context context) { + int[] screenSize = getScreenSize(context); + return Math.max(screenSize[0], screenSize[1]); + } + + /** + * 获取屏幕的短边(即竖屏下的宽,横屏下的高) + */ + public static int getScreenShortSide(Context context) { + int[] screenSize = getScreenSize(context); + return Math.min(screenSize[0], screenSize[1]); + } + + /** + * 获取状态栏高度 + */ + public static int getStatusBarHeight(Context context) { + Resources resources = context.getResources(); + int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); + return resources.getDimensionPixelSize(resourceId); + } + + /** + * 获取底部导航栏高度 + */ + public static int getNavigationBarHeight(Context context) { + Resources resources = context.getResources(); + int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + return resources.getDimensionPixelSize(resourceId); + } + + /** + * 判断当前应用是否是debug模式 + */ + public static boolean isApkInDebug(Context context) { + try { + ApplicationInfo info = context.getApplicationInfo(); + return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; + } catch (Exception e) { + return false; + } + } +} diff --git a/devring/src/main/java/com/ljy/devring/util/FileUtil.java b/devring/src/main/java/com/ljy/devring/util/FileUtil.java index b87ecab..7a82d75 100644 --- a/devring/src/main/java/com/ljy/devring/util/FileUtil.java +++ b/devring/src/main/java/com/ljy/devring/util/FileUtil.java @@ -197,13 +197,13 @@ public static long calculateFileSize(File file) { } /** - * 删除文件/文件夹 - * 如果是文件夹,则会删除其下的文件以及它本身 + * 删除文件夹中的所有文件 * - * @param file file + * @param file 指定的文件夹 + * @param isDeleteSelf 是否删除文件夹本身 * @return true代表成功删除 */ - public static boolean deleteFile(File file) { + public static boolean deleteFile(File file, boolean isDeleteSelf) { if (file == null) { return true; } @@ -216,7 +216,7 @@ public static boolean deleteFile(File file) { if (null != files) { for (File subFile : files) { if (subFile.isDirectory()) { - if (!deleteFile(subFile)) { + if (!deleteFile(subFile,true)) { result = false; } } else { @@ -227,8 +227,11 @@ public static boolean deleteFile(File file) { } } } - if (!file.delete()) { - result = false; + + if (isDeleteSelf) { + if (!file.delete()) { + result = false; + } } return result; @@ -299,7 +302,7 @@ public static String getDataDirectory() { return Environment.getDataDirectory().getAbsolutePath(); } - //返回"/storage/emulated/0"目录 + //返回"/storage/emulated/0"目录(需要外部存储权限) public static String getExternalStorageDirectory() { return Environment.getExternalStorageDirectory().getAbsolutePath(); } @@ -315,6 +318,7 @@ public static String getDownloadCacheDirectory() { } /** + * (需要外部存储权限) * @param type 所放的文件的类型,传入的参数是Environment类中的DIRECTORY_XXX静态变量 * @return 返回"/storage/emulated/0/xxx"目录 * 例如传入Environment.DIRECTORY_ALARMS则返回"/storage/emulated/0/Alarms" diff --git a/devring/src/main/java/com/ljy/devring/util/FontTypeUtil.java b/devring/src/main/java/com/ljy/devring/util/FontTypeUtil.java new file mode 100644 index 0000000..eae4191 --- /dev/null +++ b/devring/src/main/java/com/ljy/devring/util/FontTypeUtil.java @@ -0,0 +1,102 @@ +package com.ljy.devring.util; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Typeface; +import android.support.annotation.NonNull; +import android.text.TextUtils; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import java.lang.reflect.Field; + +/** + * https://blog.csdn.net/ludandan1234/article/details/53330944 + * date: 2018/5/10 + * description: 修改全局字体样式的工具类 + */ + +public class FontTypeUtil { + + //------------------ 在Activity/Fragment的基类中调用即可 -------------------// + /** + *

Replace the font of specified view and it's children

+ * 通过递归批量替换某个View及其子View的字体改变Activity内部控件的字体(TextView,Button,EditText,CheckBox,RadioButton等) + * @param context + * @param fontPath assert文件夹下的字体文件路径,如 "fonts/FZLanTYJW.ttf" + */ + public static void replaceFont(@NonNull Activity context, String fontPath) { + replaceFont(getRootView(context),fontPath); + } + + + /** + *

Replace the font of specified view and it's children

+ * @param root The root view. + * @param fontPath font file path relative to 'assets' directory. + */ + public static void replaceFont(@NonNull View root, String fontPath) { + if (root == null || TextUtils.isEmpty(fontPath)) { + return; + } + + + if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font + TextView textView = (TextView)root; + int style = Typeface.NORMAL; + if (textView.getTypeface() != null) { + style = textView.getTypeface().getStyle(); + } + textView.setTypeface(createTypeface(root.getContext(), fontPath), style); + } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views + ViewGroup viewGroup = (ViewGroup) root; + for (int i = 0; i < viewGroup.getChildCount(); ++i) { + replaceFont(viewGroup.getChildAt(i), fontPath); + } + } + } + + /* + * Create a Typeface instance with your font file + */ + public static Typeface createTypeface(Context context, String fontPath) { + return Typeface.createFromAsset(context.getAssets(), fontPath); + } + + /** + * 从Activity 获取 rootView 根节点 + * @param context + * @return 当前activity布局的根节点 + */ + public static View getRootView(Activity context) + { + return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0); + } + + + //------------------ 在Application中调用即可 -------------------// + /** + * 通过改变App的系统字体替换App内部所有控件的字体(TextView,Button,EditText,CheckBox,RadioButton等) + * @param context + * @param fontPath assert文件夹下的字体文件路径,如 "fonts/FZLanTYJW.ttf" + */ + public static void replaceSystemDefaultFont(@NonNull Context context, @NonNull String fontPath) { + replaceTypefaceField("MONOSPACE", createTypeface(context, fontPath)); + } + + /** + *

Replace field in class Typeface with reflection.

+ */ + private static void replaceTypefaceField(String fieldName, Object value) { + try { + Field defaultField = Typeface.class.getDeclaredField(fieldName); + defaultField.setAccessible(true); + defaultField.set(null, value); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/devring/src/main/java/com/ljy/devring/util/ImageUtil.java b/devring/src/main/java/com/ljy/devring/util/ImageUtil.java index 7cc742e..aaedae2 100644 --- a/devring/src/main/java/com/ljy/devring/util/ImageUtil.java +++ b/devring/src/main/java/com/ljy/devring/util/ImageUtil.java @@ -140,7 +140,7 @@ public static boolean qualityCompress(Bitmap bitmap, int maxSize,File fileToSave RingLog.e("bytes1: " + (baos.toByteArray().length / 1024)+" KB"); baos.reset(); if (options > 10) { - options -= 5; + options -= 15; } else { options -= 1; } diff --git a/devring/src/main/java/com/ljy/devring/util/KeyboardUtil.java b/devring/src/main/java/com/ljy/devring/util/KeyboardUtil.java new file mode 100644 index 0000000..2adfae6 --- /dev/null +++ b/devring/src/main/java/com/ljy/devring/util/KeyboardUtil.java @@ -0,0 +1,169 @@ +package com.ljy.devring.util; + +import android.app.Activity; +import android.content.Context; +import android.content.res.Configuration; +import android.graphics.Rect; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewTreeObserver; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; + +/** + * author: ljy + * date: 2018/5/10 + * description: 输入键盘工具类 + */ + +public class KeyboardUtil { + + /** + * 解决软键盘挡住输入框的问题(横屏的话键盘默认会全屏显示,但这样会导致失效,所以需在EditText中加入android:imeOptions="flagNoExtractUi") + * + * @param activity 页面activity + * @param isFullScreen 页面是否为全屏模式 + */ + public static void fixInput(final Activity activity, final boolean isFullScreen) { + //一些定制的EditText(如输入内容后显示删除图标)在设置setCompoundDrawablesWithIntrinsicBounds显示图标时会反复回调onGlobalLayout + //所以需要加此标志位,防止异常的反复的scroll + final boolean[] isKeyboardShowed = {false}; + //页面的根视图 + final View viewRoot = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0); + //存放根视图当前可见区域的位置信息 + final Rect rectRootView = new Rect(); + //存放当前焦点所在EditText的位置信息 + final int[] location = new int[2]; + //屏幕高度 + int screenHeight = 0; + if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { + screenHeight = ConfigUtil.getScreenShortSide(activity); + } else if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { + screenHeight = ConfigUtil.getScreenLongSide(activity); + } + final int finalScreenHeight = screenHeight; + //状态栏高度 + final int statusbarHeight = ConfigUtil.getStatusBarHeight(activity); + //导航栏高度 + final int navigationBarHeight = ConfigUtil.getNavigationBarHeight(activity); + + viewRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + + viewRoot.getWindowVisibleDisplayFrame(rectRootView);//存放根视图当前可见区域的位置信息 + View view = activity.getCurrentFocus(); + if (view != null) view.getLocationInWindow(location);//存放最下方view的位置信息 + int invisibleHeight = viewRoot.getHeight() - rectRootView.bottom;//计算不可视区域(键盘高度),用main.getHeight(),否则部分机型会异常 + + //invisibleHeight大于屏幕高度的三分之一,则视为键盘弹起了 + if (!isKeyboardShowed[0] && invisibleHeight > finalScreenHeight / 3) { + isKeyboardShowed[0] = true; + + int editTextBottom = location[1]; + if (view != null) { + editTextBottom = location[1] + view.getHeight(); + } + + //如果键盘弹起后并没有高过需要显示的最下方View,则没必要滚动布局 + if (rectRootView.bottom < editTextBottom) { + //获取scroll的窗体坐标,计算出main需要滚动的高度( 额外+10会美观些 ) + int scrollHeight = (editTextBottom + 10) - rectRootView.bottom; + //让界面整体上移的高度 +// viewRoot.scrollBy(0, scrollHeight); //使用这种方式的话,如果viewRoot是scrollview,那么要确保srcollview可滚动的空间足够 + viewRoot.setTranslationY(viewRoot.getTranslationY()-scrollHeight); + } + } + //invisibleHeight小于状态栏导航栏之和,则视为键盘收起了。(主要是考虑全屏模式) + else if (isKeyboardShowed[0] && invisibleHeight <= navigationBarHeight + statusbarHeight) { + isKeyboardShowed[0] = false; + + //移回原来的位置 +// viewRoot.scrollTo(0, 0); //使用这种方式的话,如果viewRoot是scrollview,那么要确保srcollview可滚动的空间足够 + viewRoot.setTranslationY(0); + + //全屏模式下弹出键盘再收起,并不会自动恢复回全屏,所以重新设置 + if (isFullScreen) { + //恢复全屏模式 + ColorBar.newHideBuilder().applyNav(true).build(activity).apply(); + } + } + } + + }); + } + + /** + * 根据输入法状态打开或隐藏输入法 + * + * @param context 上下文 + */ + public static void toggleKeyboard(Context context) { + InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); + } + + /** + * 隐藏输入键盘 + * + * @param context + * @param view + */ + public static void hideKeyboard(Context context, View view) { + if (view != null) { + InputMethodManager inputmanger = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + if (inputmanger != null) inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0); + } + } + + /** + * 根据触摸事件判断释放应该收起键盘 + * + * @param activity 页面 + * @param event 触摸事件 + * @param viewNotHide 触摸后不需收起键盘的view + * @return + */ + public static boolean handleKeyboardHide(Activity activity, MotionEvent event, View... viewNotHide) { + if (event.getAction() == MotionEvent.ACTION_UP) { + View viewCheck; + for (int i = 0; i < viewNotHide.length; i++) { + viewCheck = viewNotHide[i]; + if (viewCheck.isClickable() && isViewTouch(viewCheck, event)) { + return false; + } + } + + View viewFocus = activity.getCurrentFocus(); + if (viewFocus != null && (viewFocus instanceof EditText)) { + if (isViewTouch(viewFocus, event)) { + return false; + } else { + hideKeyboard(activity, viewFocus); + return true; + } + } + } + return false; + } + + /** + * 指定的View是否在触摸事件内 + */ + public static boolean isViewTouch(View view, MotionEvent event) { + int x = (int) event.getRawX(); + int y = (int) event.getRawY(); + int[] location = new int[2]; + view.getLocationOnScreen(location); + int left = location[0]; + int top = location[1]; + int right = left + view.getMeasuredWidth(); + int bottom = top + view.getMeasuredHeight(); + + if (y >= top && y <= bottom && x >= left && x <= right) { + return true; + } + return false; + } +} diff --git a/gradle.properties b/gradle.properties index 275220f..9dd06ca 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,7 +20,7 @@ KEY_ALIAS=dev_base_test KEY_PASSWORD=123456 STORE_PASSWORD=123456 -VERSION=1.0.11 +VERSION=1.0.13 GROUP=com.ljy.ring SITE_URL=https://github.com/LJYcoder/DevRing