以编程方式显示键盘 Android

its*_*bil 1 android android-softkeyboard

有没有办法在 Android O 版本以上的 Activity 中显示键盘?我有以下代码:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Run Code Online (Sandbox Code Playgroud)

它“仅”适用于以下 Android O 版本。

Reg*_*gex 7

我有两个选择给你。首先,您可以使用RequestFocus

editText.requestFocus();
Run Code Online (Sandbox Code Playgroud)

其次,您可以执行此代码来显示特定 EditText 的键盘

android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)

额外一点,用它来隐藏键盘:

android.view.View view = this.getCurrentFocus();   
android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)

注意:这里的每个代码都经过测试并且可以工作。