Android hide and show soft keyboard method

The ultimate way to hide the soft keyboard:

public class SoftKeyboardUtil {

    /**
     * Hide soft keyboard (only for Activity, not for Fragment)
     */
    public static void hideSoftKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * Hide soft keyboard (available for Activity, Fragment)
     */
    public static void hideSoftKeyboard(Context context, List<View> viewList) {
        if (viewList == null) return;

        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);

        for (View v : viewList) {
            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}

What is the parameter of the second method of  List<View> viewList SoftKeyboardUtil? What needs to be placed in the viewList are all the controls that trigger the pop-up of the soft keyboard on the current interface. For example, a login interface has an account input box and a password input box. When the keyboard needs to be hidden, the two input box objects are placed in the viewList and passed as parameters to the hideSoftKeyboard method.

 

The following method will pop up hidden, hidden popup

public  static  void hideKeyboard () {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}

 

See API for details:

\android-sdk\sources\android-26\android\view\inputmethod\InputMethodManager.java


 

Android manually show and hide soft keyboard

https://blog.csdn.net/changsimeng/article/details/72853760

 

1. Method 1 (if the input method has been displayed on the window, it will be hidden, otherwise it will be displayed)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 

2. Method 2 (view is the view that accepts soft keyboard input, SHOW_FORCED means forced display)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);

imm.hideSoftInputFromWindow(view.getWindowToken(), 0); // Forcibly hide the keyboard

3. Call the default input method of the hidden system

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this
.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); // (WidgetSearchActivity is the current Activity)

4. Get the state that the input method is open

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
 boolean isOpen=imm.isActive(); // if isOpen returns true, it means the input method is open

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028405&siteId=291194637