Kotlin when multiple values ​​don't work when value is android view

I have implemented the function that is used in anko by recursively applying:

fun applyTemplateViewStyles(view: View) {
    when(view) {
        is EditText, TextView -> {
            ....
        }
    }
}

      

And I get the error "Function invocation" TextView (...) "expected"

So how can I write when with a sentence like 0, 1 why can't I do the same with Android View?

+3


source to share


2 answers


You are missing another is

:



fun applyTemplateViewStyles(view: View) {
    when(view) {
        is EditText, is TextView -> {
            println("view is either EditText or TextView")
        }
        else -> {
            println("view is something else")
        }
    }
}

      

+7


source


You can do this, you just didn't get the syntax right. The following works to handle multiple types under one branch when

:



when(view) {
    is EditText, is TextView -> {
        ....
    }
}

      

+4


source







All Articles