3 回答

TA貢獻(xiàn)1842條經(jīng)驗 獲得超13個贊
您可以強(qiáng)制Android使用InputMethodManager隱藏虛擬鍵盤,調(diào)用hideSoftInputFromWindow
并傳入包含焦點視圖的窗口的標(biāo)記。
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
這將強(qiáng)制鍵盤在所有情況下都被隱藏。在某些情況下,您需要傳入InputMethodManager.HIDE_IMPLICIT_ONLY第二個參數(shù),以確保僅在用戶未明確強(qiáng)制顯示鍵盤時隱藏鍵盤(通過按住菜單)。
注意:如果您想在Kotlin中執(zhí)行此操作,請使用: context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
Kotlin語法
// Check if no view has focus:
val view = this.currentFocus
view?.let { v ->
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.let { it.hideSoftInputFromWindow(v.windowToken, 0) }
}

TA貢獻(xiàn)1824條經(jīng)驗 獲得超5個贊
隱藏軟鍵盤也很有用:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
這可用于在用戶實際觸摸editText視圖之前抑制軟鍵盤。
- 3 回答
- 0 關(guān)注
- 2965 瀏覽
添加回答
舉報