简体   繁体   中英

Kotlin Android Studio - onClickListener using apply plugin: 'kotlin-android-extensions'

I'm trying to create a onClickListener eventhandler that handles a button click. In my articlerecycler_item layout file I have:

<Button
    android:id="@+id/btnSave"
    android:layout_width="70dp"
    android:layout_height="40dp"
    android:onClick="btnSave"
    android:text="@string/save"
    android:textAlignment="center" />

Then in my ArticleAdapter.kt file I have:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 

ArticleViewHolder {
      val view = LayoutInflater.from(parent.context).inflate(R.layout.articlerecycler_item, parent, false)
      return ArticleViewHolder(view)

      listener.setOnClickListener(this)

  }
    /** Called when the user taps the Save button  */
    override fun onClick(v: View?){
        (R.layout.articlerecycler_item)
        AlertDialog.Builder(this)
                .setMessage("Article Saved.")
                .create()
                .show()
        // Do something in response to button click
    }

My question is what to use instead of findViewById since that is no longer needed because of the plugin: apply plugin: 'kotlin-android-extensions'

You can reference that view simply as articlerecycler_item . You only need to import it with alt + enter.

Why make everything harder? It's much easier to use.

btnSave.setOnClickListener{
    // your code goes here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM