简体   繁体   中英

Why I need to place my click listeners in onCreate and not in my Adapter

我是android新手,碰到了什么东西,为什么我必须将clicklisteners放在Create方法上,而不是放在适配器中?

your adapter can also have onclick listeners if you are using a custom adapter. and you should always put your onclicklisteners in oncreate because your buttons should listen to the clicks after the activity is created

or you can try using implements View.OnClickListener after extends activity then implement the methods to have a much more cleaner code

please refer to this site

https://anujarosha.wordpress.com/2011/11/13/how-to-implements-onclicklistener-for-a-view-item-in-android/

provides a tutorial on how to use the cleaner way

Listener can be set or clear as soon as the operation is done on a object with a valid state (eg a not null referenced button)

so you are not technically forced to set the onClicklistener in the onCreate method, normally it is done in that method because is in there where you initialize/prepare the widgets of the activity so after that preparation step your button is "ready to go"

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