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
There are a couple reasons why you might want to programmatically set an OnClickListener. The first is if you ever want to change the behavior of your button while your app is running. You can point your button at another method entirely, or just disable the button by setting an OnClickListener that doesn't do anything.
When you define a listener using the onClick attribute, the view looks for a method with that name only in its host activity. Programmatically setting an OnClickListener allows you to control a button's behavior from somewhere other than its host activity. This will become very relevant when we learn about Fragments, which are basically mini activities, allowing you to build reusable collections of views with their own lifecycle, which can then be assembled into activities. Fragments always need to use OnClickListeners to control their buttons, since they're not Activities, and won't be searched for listeners defined in onClick.
I am assuming by
onClick
that you use is the one that you defines in XML Layout. These two are alternative that serve same function but implemented differently.The
onClick
with function binding in XML Layout is a binding between onClick and the function that it will call. The function have to have one argument (theView
) in order for onClick to function.An
OnClickListener
is an interface that any class could implement. Since it is an interface that any class could implement, this has more flexibility and more complex in its form. Few flexibilities that you could have withOnClickListener
OnClickListener
enable you to separate the action/behavior of the click event from theView
that triggers the event. While for simple cases this is not such a big deal, for complex event handling, this could mean better readability and maintainability of the codeOnClickListener
is an interface, the class that implements it has flexibilities in determining the instance variables and methods that it needs in order to handle the event. Again, this is not a big deal in simple cases, but for complex cases, we don't want to necessary mix up the variables/methods that related to event handling with the code of the View that triggers the event.We use
to define a method out of the class. But To define a component Click event within a class, we use onclick listener.
OnClickListener is what waits for someone to actually click, onclick determines what happens when someone clicks
the listener is a class, the onclick is a method, this distinction is not very useful in simple cases, but if you want to be more complicated it becomes more necessary
OnClickListener
is the interface you need to implement and can be set to a view in java code.Lately android added a xml attribute to views called
android:onclick
, that can be used to handle clicks directly in the view's activity without need to implement any interface.Both function the same way, just that one gets set through java code and the other through xml code.
There are a couple reasons why you might want to programmatically set an
OnClickListener
. The first is if you ever want to change the behavior of your button while your app is running. You can point your button at another method entirely, or just disable the button by setting anOnClickListener
that doesn't do anything.When you define a listener using the
onClick
attribute, the view looks for a method with that name only in its host activity. Programmatically setting anOnClickListener
allows you to control a button's behavior from somewhere other than its host activity. This will become very relevant forFragments
,Fragments
always need to useOnClickListeners
to control their buttons, since they're not Activities, and won't be searched for listeners defined inonClick
.