有两种方法:showSystemKeyboard和hideSystemKeyboard。我认为到目前为止一切都是合乎逻辑的,一种方法显示键盘,另一种方法隐藏它。
我在 onStart() 方法中调用键盘显示showSystemKeyboard方法:
@Override
public void onStart() {
super.onStart();
if (allAvailableTags.isEmpty() && selectedTags.isEmpty()) {
Utils.showSystemKeyboard(tagNameInputView);
}
}
方法本身showSystemKeyboard:
public static void showSystemKeyboard(EditText view) {
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
任务是在我的 TagDialog 类中调用对话框时,其中实现了 onStart () 方法,键盘立即打开。我会立即进行预订,SHOW_FORCED 常量不会立即适合。谁在乎为什么,跟着这里。
试过这样的:
tagNameInputView.requestFocus();
tagNameInputView.postDelayed(new Runnable() {
@Override
public void run() {
if (allAvailableTags.isEmpty() && selectedTags.isEmpty()) {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(tagNameInputView, 0);
}
}
},200);
tagNameInputView.requestFocus();
tagNameInputView.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(tagNameInputView.getWindowToken(), 0);
}
},200);
但是我仍然必须在键盘打开之前单击该字段。
之后,您需要在 EditText 的正确位置调用 requestFocus() ,键盘将打开。
谢谢, YuriySPB 的回答派上用场了。
我在onCreateView方法中实现如下: