Android dismiss keyboard

后端 未结 9 494
面向向阳花
面向向阳花 2021-01-29 23:51

How do I dismiss the keyboard when a button is pressed?

相关标签:
9条回答
  • you can also use this code on button click event

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    
    0 讨论(0)
  • 2021-01-30 00:15
        public static void hideSoftInput(Activity activity) {
        try {
            if (activity == null || activity.isFinishing()) return;
            Window window = activity.getWindow();
            if (window == null) return;
            View view = window.getCurrentFocus();
            //give decorView a chance
            if (view == null) view = window.getDecorView();
            if (view == null) return;
    
            InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm == null || !imm.isActive()) return;
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2021-01-30 00:17

    This is my solution

    public static void hideKeyboard(Activity activity) {
        View v = activity.getWindow().getCurrentFocus();
        if (v != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }
    
    0 讨论(0)
  • 2021-01-30 00:20

    By using the context of the view, we can achieve the desired outcome with the following extension methods in Kotlin:

    /**
     * Get the [InputMethodManager] using some [Context].
     */
    fun Context.getInputMethodManager(): InputMethodManager {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return getSystemService(InputMethodManager::class.java)
        }
    
        return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    }
    
    /**
     * Dismiss soft input (keyboard) from the window using a [View] context.
     */
    fun View.dismissKeyboard() = context
            .getInputMethodManager()
            .hideSoftInputFromWindow(
                    windowToken
                    , 0
            )
    

    Once these are in place, just call:

    editTextFoo.dismiss()
    
    0 讨论(0)
  • 2021-01-30 00:22

    You want to disable or dismiss a virtual Keyboard?

    If you want to just dismiss it you can use the following lines of code in your button's on click Event

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    
    0 讨论(0)
  • 2021-01-30 00:24

    This Solution make sure that it hides keyboard also do nothing if it not opened. It uses extension so it can be used from any Context Owner class.

    
    fun Context.dismissKeyboard() {
        val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
        val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
        val height = windowHeightMethod.invoke(imm) as Int
        if (height > 0) {
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
        }
    }
    
    
    
    0 讨论(0)
提交回复
热议问题