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

尝试在 kotlin anko 中添加 textInputLayout 和 textInputEditText 时出现上述错误消息。
下面是我的代码 -

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

2 回答 2

4

在您希望的任何地方创建一个 kotlin 代码文件。在没有任何类声明的情况下放置下面的代码。

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)  

希望这可以帮助

编辑:根据@A Boschman 的评论,anko 非常新,几乎没有文档。当我遇到同样的问题并且我在互联网上搜索了很多但没有找到任何文档时。
我的解决方案基于下面提到的 GitHub 上的线程 - (也没有理论解释)

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

于 2017-07-10T08:17:33.793 回答
3

TextInputLayout位于设计支持库中,因此您需要一个额外的 Anko 依赖项来为您提供该库的绑定(您可以在此处找到所有不同 Anko 包的列表):

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

如果您还没有设计支持库,您还需要它:

compile 'com.android.support:design:25.4.0'
于 2017-07-10T07:34:34.350 回答