如何隐藏键盘。在onCreate()之前,活动无法使用系统服务

时间:2019-06-29 14:42:36

标签: android kotlin android-softkeyboard

onCreate()之前的活动无法使用系统服务

我有对话框类MyPersonalDialog(mContext:Context),并且此对话框包含EditText。 我通过在其中解析上下文来初始化MyPersonalDialog类。 val myPersonalDialog = MyPersonalDialog(this)

然后我在对话框中打电话 myPersonalDialog.showMyDialog

此类:

class MyPersonalDialog(mContext: Context){

    fun showMyDialog(){
    val builder = AlertDialog.Builder(context)
    val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val view = layoutInflater.inflate(R.layout.dialog_edit_list_title, null)
    view.renameListTitle.requestFocus()
    val inputHelper = InputHelper(context)
    inputHelper.showDialogKeyboard()
    builder.setView(view)
    builder.setNegativeButton(R.string.cancel, { dialogInterface: DialogInterface, i: Int ->
        inputHelper.hideKeyboard(activity, view)
    })
    //some other code goes next
}

}

当用户按下NegativeButton按钮hideKeyboard开始工作

class InputHelper(val context: Context){
fun hideKeyboard(activity: Activity, view: View) {
        val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}

但是出现此错误: java.lang.IllegalStateException:onCreate()之前的活动无法使用系统服务

如何解决此问题?我不明白为什么会出现此错误,因为MyPersonalDialog类是在onCreate之后启动的

找到解决方法:

class InputHelper(val context: Context){
fun showDialogKeyboard() {
    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}

fun hideKeyboard(view: View) {
        val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}}

4 个答案:

答案 0 :(得分:1)

尝试更换     fun hideKeyboard(activity: Activity, view: View) { val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) } }中的InputHelper

答案 1 :(得分:0)

将此添加到清单的“活动”中-

android:windowSoftInputMode =“ stateHiddenAlways”

答案 2 :(得分:0)

 fun hideKeyboard(activity: Activity) {
        val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        //Find the currently focused view, so we can grab the correct window token from it.
        var view = activity.currentFocus
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = View(activity)
        }
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

答案 3 :(得分:0)

recyclerViewOfLists = RecyclerViewOfLists(cursor, this, MainActivity())

您不能仅通过调用构造函数来实例化活动-例如,在这里您要实例化新的MainActivity。此类活动并未针对您需要进行的任何活动进行初始化,例如访问系统服务或以其他方式用作Context

例如,使用this将引用传递给现有实例,而不是创建新实例。

recyclerViewOfLists = RecyclerViewOfLists(cursor, this, this)