android - editText 处于焦点,但虚拟键盘不会在 Alert Builder 中使用 SHOW IMPLICIT 自动打开

标签 android android-studio kotlin

当我使用 SHOW_FORCED 时键盘打开,但是当我关闭 alertDialog 时,键盘变为文本布局并且不隐藏, 键盘仅在按下android的后退按钮时关闭,代码中模拟后退按钮不起作用。

并且使用 SHOW_IMPLICIT 不会自动打开键盘。

***注释代码有效但是...

private fun insertItemQuantity(orderDetail: OrderDetail) {
    val modal = alert {
        customView {
            verticalLayout {
                title = getString(R.string.insert_quantity)
                val quantity = editText {
                    keyListener = DigitsKeyListener.getInstance("0123456789")
                    inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
                            InputType.TYPE_NUMBER_FLAG_SIGNED
                    showSoftInputOnFocus = true
                    isFocusable = true
                    isFocusableInTouchMode = true
                }
                positiveButton(getString(R.string.confirm)) {
                    val c = quantity.text.toString()
                    if (c.isBlank() )
                        toastCustomWarning(getString(R.string.field_cannot_be_empty))
                    else {
                        if (c.toDouble() > 0) {
                            RealmRepository.getRealm().beginTransaction()
                            orderDetail.quantity = c.toDouble()
                            orderDetail.uuid = UUID.randomUUID().toString()
                            RealmRepository.getRealm().commitTransaction()
                            orderItemActivityViewModel.addOrderItem(orderDetail)
                        } else {
                            toastCustomWarning(getString(R.string.field_cannot_be_empty_or_zero))
                        }
                    }
                }
                negativeButton(getString(R.string.cancel)) {
                    quantity.clearFocus()
                }
                neutralPressed("Neutral"){

                }
            }
        }
    }

    //quantity.requestFocus()
    /*val inputManager: InputMethodManager =
            getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputManager.toggleSoftInput(
            InputMethodManager.SHOW_FORCED,
            InputMethodManager.HIDE_IMPLICIT_ONLY
    )*/
    var inputMethodManager: InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.showSoftInput(quantity, InputMethodManager.SHOW_IMPLICIT)
    //inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
    modal.iconResource = R.drawable.ic_logo
    modal.show()
}

最佳答案

我不确定如何打开键盘,但您可以使用这种方法来隐藏键盘。我工作得很好。

(getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager)
        ?.hideSoftInputFromWindow(findViewById<View>(android.R.id.content)?.windowToken, 0)

关于android - editText 处于焦点,但虚拟键盘不会在 Alert Builder 中使用 SHOW IMPLICIT 自动打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54610599/

相关文章:

android - 保护 Lucky Patcher/play 许可的方法

android - 无法访问ActivityCompatApi23的ActivityCompatApi23类文件

android - 将 AsyncTask 与来自数据库的 JSON 请求一起使用

Android Studio 0.8.14 无法解析符号 'Bundle'

android - 如何使用嵌套的挂起函数对 kotlin 协程进行单元测试

android - 如何从 Android 应用程序向 Facebook Messenger 分享纯文本

android - 膨胀类 android.support.v7.app.AlertController.RecycleListView 时出错

android - 在UdemyStudio中运行项目时出错-提及错误的JVM

java - Intellij断点报告方法抛出 'java.lang.ClassNotFoundException'异常即使变量存在

gradle - 如何修复 gradle/kotlin/netty 项目中 io.ktor.server.engine.CommandLineKt 抛出的 'Neither port nor sslPort specified'?