Android Soft Keyboard Processing

When it bounces

  • The first parameter, View, is preferably EditText or its subclasses.
  • The layout must be loaded. (View.postDelayed() is a solution to solve this problem by delayed loading)

Hidden time

  • Need to depend on a view
  • Or through delayed processing to complete, it will be effective

If it still doesn't work, make it a little longer, say 300 ms.

Processing TODO of Soft Keyboard Hiding Components

https://www.jianshu.com/p/89eec61fa699

/**
     * Soft keyboard display
     *
     * @param view
     */
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public static void showSoftKeyboard(final View view) {
        if (view == null) {
            return;
        }
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        if (!view.isFocused()) {
            view.requestFocus();
        }

        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager inputMethodManager = (InputMethodManager) view.getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.showSoftInput(view, 0);
            }
        }, 100);
    }
/**
     * Soft Keyboard Hiding
     *
     * @param view
     */

   public static void hideSoftKeyboard(final View view) {
        if (view == null) {
            return;
        }

        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager manager = (InputMethodManager) view.getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 100);
    }
/**
 * Select 2/3 of the screen height to determine whether the soft keyboard pops up or not
 * @param context
 * @return
 */
public static boolean isSoftShowing(Context context) {
    // Get the height of the current screen content
    int screenHeight = ((Activity)context).getWindow().getDecorView().getHeight();
    // Get the bottom of the View visible area
    Rect rect = new Rect();
    // DecorView is the top view of activity
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
        ((Activity)context).getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
    }
    // Consider the virtual navigation bar (screen Height = rect. bottom + virtual navigation bar height)
    // Select screen Height*2/3 for judgment
    return screenHeight*2/3 > rect.bottom;
}

Added by wjwolf79 on Mon, 30 Sep 2019 14:28:09 +0300