Android Studio 3.0 之 Kotlin中使用ButterKnife

  1. 在module的build.gradle中添加:
kapt 'com.jakewharton:butterknife-compiler:8.8.1'
  1. 在Activity中添加:
@BindView(R.id.tv_greeting)
lateinit var greetingTv: TextView
.....
ButterKnife.bind(this)
greetingTv.text = "Hello Kotlin"
  1. 绑定点击事件:
@OnClick(R.id.tv_button)
    internal fun onClick(v : View) {
        when(v.id) {
             R.id.tv_button ->
                     Toast.makeText(this, "Button is clicked!", Toast.LENGTH_LONG).show()
        }
    }

你可能感兴趣的:(Android Studio 3.0 之 Kotlin中使用ButterKnife)