android 軟鍵盤彈出隱藏

控制軟鍵盤彈出和隱藏

    protected fun showKeyboard(isShow: Boolean , view :EditText  ) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if (isShow) {
            view.setFocusable(true)
            view.setFocusableInTouchMode(true)
            view.requestFocus()
            if (view == null) {
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
            } else {
                imm.showSoftInput(view, 0)
            }
        } else {
            if (view != null) {
                imm.hideSoftInputFromWindow(view!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
            }
        }
    }

 

edittext 獲取焦點


EditText.setFocusable(true);

EditText.setFocusableInTouchMode(true);

 

 EditText自動獲取焦點並彈出鍵盤


EditText.setFocusable(true);

EditText.setFocusableInTouchMode(true);

EditText.requestFocus();

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章