简体   繁体   中英

Kotlin Cast EditText, TextView to View

i have one static method in kotlin to hide soft keyboard, it works in java if i pass EditText, TextView as second parameter of method.

But in kotlin it gives error,

Error:(56, 71) Type mismatch: inferred type is EditText? but View was expected

i tried to change View to EditText in function, but it doesn't work for TextView

Also tried to change View to Any, but applicationWindowToken gives error.

This is common function in app.

companion object {
        fun hideSoftKeyboard(activity: Activity, view: View) {
            val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.applicationWindowToken, 0)
        }
    }

Try to change code View to View?

companion object {
        fun hideSoftKeyboard(activity: Activity, view: View?) {
            view?.let {
                 val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(it.applicationWindowToken, 0)
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM