KeyboardHelper.java 3.57 KB
package com.people.speech.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * @author LiuKun
 * @date 2023/5/2  16:58
 * @Description:对软键盘的操作
 */
public class KeyboardHelper {

    /**
     * 编辑框获取焦点,并显示软件盘
     */
    public static void showSoftInput(View view) {
        if (view == null) {
            return;
        }
        view.requestFocus();
        InputMethodManager manager = (InputMethodManager) view.getContext().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.showSoftInput(view, 0);
    }

    /**
     * 隐藏软件盘
     */
    public static void hideSoftInput(View view) {
        if (view == null) {
            return;
        }
        InputMethodManager manager = (InputMethodManager) view.getContext().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 是否显示软件盘
     *
     * @return
     */
    public static boolean isSoftInputShown(Activity activity) {
        return getSupportSoftInputHeight(activity) != 0;
    }

    /**
     * 获取软件盘的高度
     *
     * @return
     */
    private static int getSupportSoftInputHeight(Activity activity) {
        Rect r = new Rect();
        /**
         * decorView是window中的最顶层view,可以从window中通过getDecorView获取到decorView。
         * 通过decorView获取到程序显示的区域,包括标题栏,但不包括状态栏。
         */
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
        //获取屏幕的高度
        int screenHeight = activity.getWindow().getDecorView().getRootView().getHeight();
        //计算软件盘的高度
        int softInputHeight = screenHeight - r.bottom;

        /**
         * 某些Android版本下,没有显示软键盘时减出来的高度总是144,而不是零,
         * 这是因为高度是包括了虚拟按键栏的(例如华为系列),所以在API Level高于20时,
         * 我们需要减去底部虚拟按键栏的高度(如果有的话)
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            // When SDK Level >= 20 (Android L), the softInputHeight will contain the height of softButtonsBar (if has)
            softInputHeight = softInputHeight - getSoftButtonsBarHeight(activity);
        }

        if (softInputHeight < 0) {
            Log.d("123", "EmotionKeyboard--Warning: value of softInputHeight is below zero!");
        }
        return softInputHeight;
    }

    /**
     * 底部虚拟按键栏的高度
     *
     * @return
     */
    private static int getSoftButtonsBarHeight(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        //这个方法获取可能不是真实屏幕的高度
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        //获取当前屏幕的真实高度
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        }
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight) {
            return realHeight - usableHeight;
        } else {
            return 0;
        }
    }


}