【问题标题】:keyboard is not shown after pause or stop fragment暂停或停止片段后不显示键盘
【发布时间】:2020-01-05 09:03:45
【问题描述】:

Android 不会在焦点编辑文本中显示键盘,即使在暂停或停止片段之后单击它也是如此。例如,我专注于显示编辑文本键盘,一切正常,然后我在恢复相同的片段后单击主页按钮(或只是离开片段),焦点仍在编辑文本中,但无法再次显示键盘.来自日志:

W/InputMethodManager: 当前服务视图不是焦点视图

我只有一项活动,清单中有以下信息:

活动 android:name=".presentation.main.MainActivity" android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait">

在某些情况下,我们需要手动关闭键盘并使用以下广播:

  val view = this@MainActivity.currentFocus
            if (view != null) {
                val imm =
                    this@MainActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(view.windowToken, 0)
            }

【问题讨论】:

  • onResume 方法中检查当前焦点是否在所需的EditText 上然后生成editText.performClick() 方法
  • 我不手动点击edittext字段但没有意义,只是从日志中得到消息(有问题提到)

标签: android kotlin keyboard broadcastreceiver


【解决方案1】:

要在 EditText 上强制打开键盘,请使用以下代码

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

如果还是不行的话

editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

【讨论】:

  • if (focusedView != null) {focusedView?.postDelayed({ val editText = if (focusedView is CustomSearchField) {focusedView?.findViewById(R.id.searchField) } else if ( focusedView 是 AppCompatEditText) { focusView } else { null } val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT) }, 200) }
  • 我在 onStart 方法中添加了这段代码,但没有意义 :((((( override fun onPause() {focusedView = this.currentFocus super.onPause() }
  • @Nininea 你为什么要写在onStart 方法里面。将此代码贴在onResume 方法中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
相关资源
最近更新 更多