我喜欢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.*
会有帮助吗? - EpicPandaForce