隐藏虚拟按键,并且全屏
使用:
ScreenUtils.hideBottomUIMenu(getWindow().getDecorView());
/*** 隐藏虚拟按键,并且全屏*/public static void hideBottomUIMenu(View view) {//隐藏虚拟按键,并且全屏if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {// lower apiview.setSystemUiVisibility(View.GONE);} else if (Build.VERSION.SDK_INT >= 19) {//for new api versions.int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_FULLSCREEN| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;view.setSystemUiVisibility(uiOptions);}}
获取配置
BaseApplication举例写法
第一个参数为:在自己的Application中传参进去;
第二个参数为:获取当前是测试服还是正式服;
如:UtilConfig.initUtilConfig(this, BuildConfig.DEBUG);
public class UtilConfig {private static Application application;public static boolean DEBUG = BuildConfig.DEBUG;public static void initUtilConfig(Application application, boolean isDebug) {UtilConfig.application = application;UtilConfig.DEBUG = isDebug;}public static Application getApplication() {return application;}}
获取屏幕宽高
public static int getScreenWidth() {WindowManager wm = (WindowManager) UtilConfig.getApplication().getSystemService(Context.WINDOW_SERVICE);if (wm == null) return -1;Point point = new Point();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {wm.getDefaultDisplay().getRealSize(point);} else {wm.getDefaultDisplay().getSize(point);}return point.x;}public static int getScreenHeight() {WindowManager wm = (WindowManager) UtilConfig.getApplication().getSystemService(Context.WINDOW_SERVICE);if (wm == null) return -1;Point point = new Point();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {wm.getDefaultDisplay().getRealSize(point);} else {wm.getDefaultDisplay().getSize(point);}return point.y;}
判断是否是横屏
return UtilConfig.getApplication().getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE;
获取状态栏的高度
public static int getStatueBarHeight() {int height = DimenUtil.dp2px(22);try {int resourceId = WeApplication.getInstance().getResources().getIdentifier("status_bar_height", "dimen", "android");if (resourceId > 0) {height = WeApplication.getInstance().getResources().getDimensionPixelSize(resourceId);}} catch (Throwable e) {e.printStackTrace();}return height;}
关于软键盘的工具类
使用方法:
显示:KeyboardUtils.show(edittext);隐藏:KeyboardUtils.hideSoftInput(edittext);将字符串复制到粘贴板:copyTextToClipboard软键盘显示时将view推上去:controlKeyboardLayout
public final class KeyboardUtils {private KeyboardUtils() {throw new UnsupportedOperationException("u can't instantiate me...");}//显示软键盘public static void show(View view) {MainHandlerUtil.postDelay(() -> {try {InputMethodManager imm =(InputMethodManager)ContextUtil.getActivity(view.getContext()).getSystemService(Context.INPUT_METHOD_SERVICE);if (imm != null) {imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);}} catch (Throwable e) {e.printStackTrace();}}, 150);}/*** Hide the soft input.** @param view The view.*/public static void hideSoftInput(@NonNull final View view) {InputMethodManager imm =(InputMethodManager) UtilConfig.getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);if (imm == null) return;imm.hideSoftInputFromWindow(view.getWindowToken(), 0);}public static void copyTextToClipboard(String text) {ClipboardManager clipboardManager = (ClipboardManager) UtilConfig.getApplication().getSystemService(Context.CLIPBOARD_SERVICE);ClipData clipData = ClipData.newPlainText("Label", text);clipboardManager.setPrimaryClip(clipData);}/*** @param context用于获取底部导航栏高度。* @param root 最外层布局* @param bottomView 可视区域的底部,即上移后的底层view*/public static void controlKeyboardLayout(Context context, final View root, final View bottomView) {final int navigationBarHeight = getNavigationBarHeight(context);root.getViewTreeObserver().addOnGlobalLayoutListener(() -> {Rect rect = new Rect();//获取root在窗体的可视区域root.getWindowVisibleDisplayFrame(rect);//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;//若不可视区域高度大于100,则键盘显示if (rootInvisibleHeight > navigationBarHeight) {int[] location = new int[2];//获取scrollToView在窗体的坐标bottomView.getLocationInWindow(location);//计算root滚动高度,使scrollToView在可见区域int scrollHeight = (location[1] + bottomView.getHeight()) - rect.bottom;if (root.getScrollY() != 0) {// 如果已经滚动,要根据上次滚动,重新计算位置。scrollHeight += root.getScrollY();}root.scrollTo(0, scrollHeight);} else {//键盘隐藏root.scrollTo(0, 0);}});}/*** 获取底部导航栏高度*/private static int getNavigationBarHeight(Context act) {Resources resources = act.getResources();int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");int height = resources.getDimensionPixelSize(resourceId);return height;}}
Toast
public class ToastUtil {static Toast toast = null;private static Handler sHandler = new Handler(Looper.getMainLooper());public static void show(final String msg) {if ("".equals(msg) || msg == null) return;sHandler.post(new Runnable() {@Overridepublic void run() {try {// 因为9以上的系统不新建toast会不显示后续的toasttoast = Toast.makeText(UtilConfig.getApplication(), msg, Toast.LENGTH_SHORT);toast.show();} catch (Throwable e) {}}});}public static void show(@StringRes int resourceId) {show(UtilConfig.getApplication().getString(resourceId));}public static void show(final String msg, Object... args) {show(String.format(msg, args));}public static void show(@StringRes int resourceId, Object... args) {show(UtilConfig.getApplication().getString(resourceId), args);}public static void cancel() {sHandler.post(new Runnable() {@Overridepublic void run() {if (toast != null) {toast.cancel();}}});}}
如果觉得《Android常见公有方法(隐藏虚拟按键/隐藏软键盘/获取屏幕宽高等)》对你有帮助,请点赞、收藏,并留下你的观点哦!