我正在尝试在触摸事件中显示一个视图的软输入键盘。 这一行是有效的:
inputManager.toggleSoftInputFromWindow(getWindowToken(),0,0);
但是这行代码不起作用:
inputManager.showSoftInput(this,0);
为什么会这样?如果我想将软输入法连接到视图上怎么办? 谢谢。
我正在尝试在触摸事件中显示一个视图的软输入键盘。 这一行是有效的:
inputManager.toggleSoftInputFromWindow(getWindowToken(),0,0);
inputManager.showSoftInput(this,0);
我想你是在模拟器上测试,而不是在真实设备上测试?
在 AVD 上它不会打开键盘,但是在真实设备上它会打开键盘,因为真实设备没有硬件键盘。
要在 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);
showSoftInput()
方法只有在你的 View
获得焦点后才能生效。此外,调用 requestFocus()
方法也不会让你的 View
获得焦点,除非你先将 setFocusableInTouchMode()
和/或 setFocusable()
设置为 true。
尝试在“onclick”事件中使用此代码。
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
您需要先请求焦点并按以下方式显示软输入:
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);
}
}
});