提供一个能够隐藏与显示软键盘以及监控软键盘状态的工具

工具:KeyBoradUtil

Kotlin 版:

object KeyBoardUtil {

    fun hideSoftInput(activity: Activity) {
        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(activity.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }

    fun showSoftInput(view: EditText) {
        val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        view.requestFocus()
        imm.showSoftInput(view, 0)
    }

    fun toggleSoftInput(activity: Activity) {
        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(0, 0)
    }

    fun onSoftInputStateChanged(activity: Activity, show: () -> Unit = {}, hide: () -> Unit = {}) {
        var preUsableHeight = 0
        val group = activity.window.decorView.findViewById<View>(android.R.id.content)
        group.viewTreeObserver.addOnGlobalLayoutListener {
            preUsableHeight = possiblyResizeChildOfContent(preUsableHeight, group, show, hide)
        }
    }

    private fun possiblyResizeChildOfContent(preUsableHeight: Int, view: View, show: () -> Unit, hide: () -> Unit): Int {
        var temp = preUsableHeight
        val usableHeightNow = computeUsableHeight(view)
        if (temp == 0) {
            temp = usableHeightNow
        }
        if (usableHeightNow < temp) {
            show()
        }
        if (usableHeightNow > temp) {
            hide()
        }
        return usableHeightNow
    }

    private fun computeUsableHeight(view: View): Int {
        val r = Rect()
        view.getWindowVisibleDisplayFrame(r)
        return r.bottom - r.top
    }
}

 

Java 版:

public class KeyBoardUtil {

    public static void hideSoftInput(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            View currentFocus = activity.getCurrentFocus();
            if (currentFocus != null) {
                imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    public static void showSoftInput(EditText view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        view.requestFocus();
        if (imm != null) {
            imm.showSoftInput(view, 0);
        }
    }

    public static void toggleSoftInput(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.toggleSoftInput(0, 0);
        }
    }

    public static void onSoftInputStateChanged(Activity activity, final OnKeyBoardStateChangedListener listener) {
        if (listener == null) {
            return;
        }
        final int[] preUsableHeight = {0};
        final View group = activity.getWindow().getDecorView().findViewById(android.R.id.content);
        group.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                preUsableHeight[0] = possiblyResizeChildOfContent(preUsableHeight[0], group, listener);
            }
        });
    }

    private static int possiblyResizeChildOfContent(int preUsableHeight, View view, OnKeyBoardStateChangedListener listener) {
        int usableHeightNow = computeUsableHeight(view);
        if (preUsableHeight == 0) {
            preUsableHeight = usableHeightNow;
        }
        if (usableHeightNow < preUsableHeight) {
            listener.onShow();
        }
        if (usableHeightNow > preUsableHeight) {
            listener.onHide();
        }
        return usableHeightNow;
    }

    private static int computeUsableHeight(View view) {
        Rect r = new Rect();
        view.getWindowVisibleDisplayFrame(r);
        return r.bottom - r.top;
    }


    interface OnKeyBoardStateChangedListener {
        /**
         * 键盘显示
         */
        void onShow();

        /**
         * 键盘隐藏
         */
        void onHide();
    }
}

   我在 Github 上创建了一个库,会有其他工具的继续更新:

   https://github.com/meetsl/CommonUtils

posted @ 2018-09-24 10:18  Spiderman.L  阅读(612)  评论(0编辑  收藏  举报