Show/Hide Soft Keyboard event in Fragment

前端 未结 2 875
予麋鹿
予麋鹿 2021-01-19 06:21

There are a lot of posts about finding a show/hide soft keyboard event. I find myself in a situation where I need to change an icon based on the soft key status, in a fragme

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 06:44

    After some struggle I was able to abstract a method that I can call whenever I need to show or hide the keyboard without using the SHOW_FORCED that I saw in many answers where the result would be the keyboard open even on a new activity without text input.

    I used this code inside onGlobalLayout to check if the keyboard is open or not and then in my method I decide if I want open or close.

    Here is the code:

    To check if it's open or not:

    private static boolean isKeyboardVisible(Activity activity) {
        Rect r = new Rect();
        View contentView = activity.findViewById(android.R.id.content);
        contentView.getWindowVisibleDisplayFrame(r);
        int screenHeight = contentView.getRootView().getHeight();
        int keypadHeight = screenHeight - r.bottom;
    
        return (keypadHeight > screenHeight * 0.15);
    }
    

    To perform the action that I need (here is where I call the above method):

    public static void toggleKeyboard(final Activity activity, final boolean showKeyboard) {
        final InputMethodManager imm =
                (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    
        // The Handler is needed because the method that checks if the keyboard
        // is open need some time to get the updated value from the activity,
        // e.g. when my activity return to foreground. 
        new Handler().postDelayed(new Runnable() {
            public void run() {
                // This 'if' just check if my app still in foreground 
                // when the code is executed to avoid any problem.
                // I've leave out of the answer to keep short, you may use your own.
                if(Tools.isAppInForeground(activity)) {
                    // Check the keyboard.
                    boolean isVisible = isKeyboardVisible(activity);
    
                    // If I want to show the keyboard and it's not visible, show it!
                    if (showKeyboard && !isVisible) {
                        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                                InputMethodManager.HIDE_IMPLICIT_ONLY);
    
                    // If I want to hide and the keyboard is visible, hide it!
                    } else if (!showKeyboard && isVisible) {
                        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
                    }
                }
            }
        }, 100);
    
    }
    

    To use, I just call like this:

    toggleKeyboard(myactivity, true); // show
    // or
    toggleKeyboard(myactivity, false); // hide
    

提交回复
热议问题