【问题标题】:Custom setter in Kotlin [duplicate]Kotlin中的自定义设置器[重复]
【发布时间】:2018-07-12 18:43:46
【问题描述】:

我想做这样的事情:

public void setContentView(int resId) {
    this.content = itemView.findViewById(resId);
}

但不知道怎么做。有什么解决方案可以在下面修复我的代码?

private var contentView: AppCompatEditText? = null
set(value: Int) {field = v.findViewById(value)} // value in setter is AppCompatEditText? type not Int

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    这不行吗?

    fun setView(resID: Int){
      foo = view.findViewById(resID)
    }
    

    编辑:您遇到什么错误?

    【讨论】:

    • 这不是 kotlin 中的二传手
    • 是的!这是一个伟大而简单的例子 :) fun setContentEditText(resId: Int) = v.findViewById(resId)
    • 是的,我只是按照您的要求“我想做这样的事情:”,这不是二传手。所以是的,一个复选标记......和一个-1。哈哈
    猜你喜欢
    • 2022-11-23
    • 2021-12-17
    • 2017-12-10
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多