2017-10-21 2 views
0

ButterKnife에서 @OnClick 속성의 가독성을 좋아합니다. 따라서 Kotlin에서도이 기능을 사용하고 있습니다. 불행히도 클릭 할 때 클릭 핸들러가 시작되지 않습니다. 내가 놓친 게 있니? kotlin에서 클릭 리스너를 통합하기 위해해야 ​​할 일이 있습니까?ButterKnife @OnClick 주석은 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> 
+0

생성 된 클래스를 살펴볼 수 있습니다. – tynn

+0

'import kotlinx.android.synthetic.main.fragment_profile. *'을 제거하면 도움이됩니까? – EpicPandaForce

답변

0

왜 당신이 이런 식으로하지 않는다?

// 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([email protected], "You clicked me.", Toast.LENGTH_SHORT).show() 
} 
+0

그러나 클릭시 바인딩되는 함수를 직접 정의 할 수 있습니까? – Rick

관련 문제