Android常见公有方法(隐藏虚拟按键/隐藏软键盘/获取屏幕宽高等)

隐藏虚拟按键,并且全屏

使用:
ScreenUtils.hideBottomUIMenu(getWindow().getDecorView());

 /**
     * 隐藏虚拟按键,并且全屏
     */
    public static void hideBottomUIMenu(View view) {
        //隐藏虚拟按键,并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            view.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() {
            @Override
            public void run() {
                try {
                    // 因为9以上的系统不新建toast会不显示后续的toast
                    toast = 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() {
            @Override
            public void run() {
                if (toast != null) {
                    toast.cancel();
                }
            }
        });
    }


}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值