I'm always using onclick()
event in most of my projects. But, I read about OnClickListener()
. Can anyone tell what's the difference between these two? And which one is best to use in Android application?.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
everyone has mentioned about
OnClickListener
listner which one always used. i want to add one more pointandroid:onClick
works as method and it's doesn't need to be reference so it's useful when you have to add button after some task executed so you cant't referenced it forOnClickListener
.For an
example
when we create viewpager with only layout (no fragments) if you put an button in any layout it insialized only when layout visible so you can't use methodfindViewById
for Button in that caseandroid:onClick
becomed useful just put that method in activity!!You can add
android:onClick="your_method"
attribute in your XML.Example:
When we want to add click listener to button in Java code, we use
OnClickListener
.When we want to add click listener to button in the layout file, we use
android:onClick="your_method"
If you use XML variant, you must implement
your_method
in your app class.I'm not sure the question is clear.
View.OnClickListener
is an interface, which defines theonClick(View)
method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.Here is the simple terminology If u are at home and U want to call someone..u can call directly and they can listen u. (use onclick). But if u are outside and u want to Call someone at home u need to use either phone or Internet.(need to use onclicklistener). In Android everything starts from home, I.e. main_activity This is the way android eases yr work ; when u have one activity u don't have to attach a listener, create object, and define it. Just use onClick. Onclicklistener are generally used in Fragments. So Keep Coding.
OnClickListener is an interface and onClick is method of OnClickListener.