【问题标题】:ButterKnife @OnClick annotation not working in Kotlin fragmentButterKnife @OnClick 注释在 Kotlin 片段中不起作用
【发布时间】:2017-10-21 21:45:38
【问题描述】:

我喜欢 ButterKnife 中 @OnClick 属性的可读性:因此,我什至在 Kotlin 中也使用它。不幸的是,当我点击时,点击处理程序并没有被触发。我错过了什么吗?我需要做些什么才能将点击监听器集成到 kotlin 中吗?

片段:

import kotlinx.android.synthetic.main.fragment_profile.*

class ProfileFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater!!.inflate(R.layout.fragment_profile, container, false)
        ButterKnife.bind(this, view)


        return view
    }

    companion object {
        fun newInstance(): ProfileFragment {
            return ProfileFragment()
        }
    }

    @OnClick(R.id.fab)
    public fun onFab() {
        Toast.makeText(activity, "ABC", Toast.LENGTH_LONG).show()
        Snackbar.make(container, "Hey there!", Snackbar.LENGTH_LONG).show()
    }
}

布局

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="300dp">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_gravity="bottom|right|end"
    android:src="@drawable/ic_edit"
    app:fabSize="normal"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp"/>

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 你可以看看生成的类。
  • 删除import kotlinx.android.synthetic.main.fragment_profile.* 有帮助吗?
  • 你找到解决办法了吗?
  • 我相信这是 Butterknife 和新的 kotlin 合成进口之间的冲突。我认为冲突可能已经解决,但就我而言,我只是使用了 Sergio 的答案。

标签: android kotlin kotlin-android-extensions


【解决方案1】:

如果您使用的是 Kotlin,请将 annotationProcessor 替换为 kapt

其背后的原因可能是您使用了这样的依赖项:

dependencies {
  implementation 'com.jakewharton:butterknife:10.1.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
}

像这样替换它:

dependencies {
  implementation 'com.jakewharton:butterknife:10.1.0'
  kapt'com.jakewharton:butterknife-compiler:10.1.0'
}

【讨论】:

    【解决方案2】:

    你为什么不这样做呢?

    // get reference to button
    val btn_click_me = findViewById(R.id.btn_click_me) as Button
    // set on-click listener
    btn_click_me.setOnClickListener {
        // your code to perform when the user clicks on the button
        Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
    }
    

    【讨论】:

    • 但是可以直接定义一个在点击时绑定的函数吗?
    • 实际上你甚至不需要在 kotlin 中使用 findViewById ,只需使用它的 id 直接访问视图按钮,就像这样 'btn_click_me.setOnClickListener{}'
    • @has19 前提是您将 apply plugin: 'kotlin-android-extensions' 添加到模块的 build.gradle 中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多