简体   繁体   English

Kotlin将EditText,TextView转换为View

[英]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. 我在kotlin中有一个静态方法来隐藏软键盘,如果我将EditText,TextView作为方法的第二个参数传递的话,它将在Java中起作用。

But in kotlin it gives error, 但是在Kotlin中,它会产生错误,

Error:(56, 71) Type mismatch: inferred type is EditText? 错误:(56,71)类型不匹配:推断的类型是EditText? but View was expected 但预期的视图

i tried to change View to EditText in function, but it doesn't work for TextView 我试图在功能中将视图更改为EditText,但不适用于TextView

Also tried to change View to Any, but applicationWindowToken gives error. 还尝试将“视图”更改为“任意”,但是applicationWindowToken给出了错误。

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? 尝试将代码View更改为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)
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TextView 不能转换为 EditText - TextView cannot be cast to EditText 无法在自定义适配器中将EditText转换为TextView - Cannot cast EditText into TextView in a custom adapter android.widget.TextView无法转换为android.widget.EditText 5 - android.widget.TextView cannot be cast to android.widget.EditText 5 无法从View转换为EditText - Can't cast from View to EditText 如何在循环中从 EditText 中提取文本并将其连接起来以在单个 TextView 中查看? - How to extract text from EditText in a loop and concatenate it to view it in a single TextView? 一起添加edittext和textview的值,并在文本视图中显示总计 - Add value of edittext and textview together and displaying total in text view Kotlin - Inflated View 键盘中的 EditText 以黑色不透明度显示在背面 - Kotlin - EditText in Inflated View keyboard show on back with black opacity java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.EditText - java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText 转换为TextView时,onClick方法的视图返回null - View of onClick method returns null when cast to TextView 调用按钮后添加两个EditText值,并使用Kotlin显示到TextView - Add two EditText values upon call of a button and display to a TextView using Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM