OnClick and onClickListener | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

OnClick and onClickListener

what would I lose if I used onClick method instead of onClickListener interface?

10th Jul 2018, 10:54 AM
Ahmed Mohamed
Ahmed Mohamed - avatar
4 Answers
+ 3
Hello, Ahmed Mohamed ! I'm not sure that the question is clear. onClickListener is the interface that defines the onClick() method. If you have a class that intends to listen for clicks, you need to implement the interface (if it does not already extend the class that does), and implement this method too. You must use both; they are not alternatives.
10th Jul 2018, 11:15 AM
Alexander Sokolov
Alexander Sokolov - avatar
+ 1
Using the event listener is generally better. It is much easier to have multiple onclick events if you use the listener, rather than the HTML attribute. You can add and remove events, without having to edit the onclick attribute (which is saved similarly to a string). However, some old browsers don't fully support the listener.
10th Jul 2018, 11:07 AM
James
James - avatar
+ 1
James I don't actually understand the multiple events part, I think I can have multiple events using the onClick method too
10th Jul 2018, 11:16 AM
Ahmed Mohamed
Ahmed Mohamed - avatar
+ 1
Alexander Sokolov i mean using the onClick attribute in xml then making a method in Java without writing the onclicklistener line of code
10th Jul 2018, 11:20 AM
Ahmed Mohamed
Ahmed Mohamed - avatar