我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
当前回答
我发现了一个奇怪的行为,因为在我的一个应用程序中,软键盘在进入活动时自动显示(onCreate中有editText.requestFocus())。
进一步挖掘,我发现这是因为布局周围有一个ScrollView。如果我删除ScrollView,行为如原始问题声明中所述:只有在单击已经聚焦的editText时,软键盘才会显示出来。
如果这对你不起作用,试着加入一个ScrollView——无论如何这是无害的。
其他回答
当我阅读官方文档时,我认为这是最好的答案,只是将视图传递给参数,如您的EditText,但showSoftKeyboard似乎不能在横向上工作
private fun showSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
private fun closeSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
以下是我从Square得到的一个更可靠的解决方案:
fun View.focusAndShowKeyboard() { /** * This is to be called when the window already has focus. */ fun View.showTheKeyboardNow() { if (isFocused) { post { // We still post the call, just in case we are being notified of the windows focus // but InputMethodManager didn't get properly setup yet. val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) } } } requestFocus() if (hasWindowFocus()) { // No need to wait for the window to get focus. showTheKeyboardNow() } else { // We need to wait until the window gets focus. viewTreeObserver.addOnWindowFocusChangeListener( object : ViewTreeObserver.OnWindowFocusChangeListener { override fun onWindowFocusChanged(hasFocus: Boolean) { // This notification will arrive just before the InputMethodManager gets set up. if (hasFocus) { this@focusAndShowKeyboard.showTheKeyboardNow() // It’s very important to remove this listener once we are done. viewTreeObserver.removeOnWindowFocusChangeListener(this) } } }) } }
代码从这里开始。
这很疯狂,但确实有效
fun showKeyboard(view: View) {
try {
InputMethodManager::class.java.getMethod(
"showSoftInputUnchecked",
Int::class.javaPrimitiveType,
ResultReceiver::class.java
).apply {
isAccessible = true
invoke(view.context.inputMethodManager, 0, null)
}
}
catch (e: Exception) {
e.printStackTrace()
}
}
只需在EditText视图中添加这一行:
android:isScrollContainer="true"
和TADA -键盘开始自动显示!
我也有类似的问题,发现了这个简单而奇怪的解决方法。
正如user3392439在这里已经提到的,键盘在焦点上的出现与XML文件中滚动组件的出现有某种奇怪的联系。
即使在同一XML中存在另一个包含上述行的EditText视图,也会使键盘出现,无论当前关注的是哪个EditText。
如果您的XML文件中至少有一个包含滚动组件的可见视图-键盘将自动显示在焦点上。
如果没有滚动-然后你需要点击EditText使键盘出现。
要强制显示软键盘,可以使用
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
为了移除EditText上的焦点,你需要一个虚拟视图来抓取焦点。
关闭它,你可以使用
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
这适用于在对话框中使用它
public void showKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public void closeKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}