[英]Show soft input keyboard
我正在尝试为触摸事件的视图显示软输入键盘。 这条线有效:
inputManager.toggleSoftInputFromWindow(getWindowToken(),0,0);
但这条线不起作用:
inputManager.showSoftInput(this,0);
为什么会这样? 如果我想将软输入连接到视图怎么办? 谢谢。
我认为你在模拟器上测试。 不是真正的设备?
它不会在AVD上打开keyboard
,但它会在没有Hard key board
真实设备上打开。
要在AVD
上测试它,您需要禁用键盘。
禁用键盘使用
Click on AVD manager > open you targeted AVD > Edit > Hardware > New > Keyboard Support > OK > Make it "NO"
尝试这个:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
在onclick事件中尝试这个。
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
除非您的View
具有焦点,否则showSoftInput()
将无法工作。 此外,除非首先将setFocusableInTouchMode()
和/或setFocusable()
调用为true,否则调用requestFocus()
不会给出View
焦点。
您需要先请求焦点并显示软输入,如下所示:
mEditTextStudy.requestFocus();
mEditTextStudy.post(
new Runnable() {
@Override
public void run() {
InputMethodManager imm =
(InputMethodManager)
getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(mEditTextStudy, SHOW_FORCED);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.