首页 > 解决方案 > 如何在开始对话框中显示键盘

问题描述

当对话框打开时,键盘被隐藏。我想在对话框打开时自动显示键盘。

 val mDialogView = LayoutInflater.from(this).inflate(R.layout.activity_profile_name_surname_dialog, null)
                val mBuilder = AlertDialog.Builder(this).setView(mDialogView).setTitle("Change Name and Surname")
                val mAlertDialog = mBuilder.show()

标签: kotlindialogkeyboard

解决方案


你可以这样。它将强制打开键盘。

   { 
            val mDialogView = LayoutInflater.from(this).inflate(R.layout.activity_profile_name_surname_dialog, null)
            val mBuilder = AlertDialog.Builder(this).setView(mDialogView).setTitle("Change Name and Surname")
            val mAlertDialog = mBuilder.show()

         mDialogView?.let { v ->

            val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm?.let { it.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0) }
        }
   }

推荐阅读