简体   繁体   中英

How to use TextInputLayout & TextInputEditText in Kotlin anko

Error:(63, 13) Unresolved reference: textInputLayout
Error:(64, 17) Unresolved reference: textInputEditText  

I'm getting above error message when trying to add textInputLayout & textInputEditText in kotlin anko.
Below is my code -

private fun test(context: Context): View{
    return with(context){
        verticalLayout {
            textInputLayout {
                textInputEditText{}
            }
        }
   }
}

Create a kotlin code file anywhere you wish. Put below code without any class declaration.

inline fun ViewManager.textInputEditText() = textInputEditText {}
inline fun ViewManager.textInputEditText(theme: Int = 0, init: TextInputEditText.() -> Unit) = ankoView({ TextInputEditText(it) }, theme, init)

inline fun ViewManager.textInputLayout() = textInputLayout {}
inline fun ViewManager.textInputLayout(theme: Int = 0, init: TextInputLayout.() -> Unit) = ankoView({ TextInputLayout(it) }, theme, init)  

Hope this helps

EDIT : based on the comment of @A Boschman, anko is very new and there is almost no documentation. When I had the same problem and I search a lot all over internet but didn't find any documentation.
My Solution is based on below mention thread on GitHub - (there also no theoritical explanation)

https://github.com/Kotlin/anko/issues/205
https://github.com/Kotlin/anko/issues/264

TextInputLayout is in the Design Support Library, so you need an additional Anko dependency that gives you bindings for that library (you can find a list of all the different Anko packages here ):

compile "org.jetbrains.anko:anko-design:$anko_version"

You'll also need the Design Support Library itself if you don't have it yet:

compile 'com.android.support:design:25.4.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