Difference between OnClick() event and OnClickListener?
I'm always using onclick()
event in most of my projects. But, I read about OnClickListener()
. Can anyone tell what's the difference bet开发者_运维技巧ween these two? And which one is best to use in Android application?.
I'm not sure the question is clear. View.OnClickListener
is an interface, which defines the onClick(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.
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.
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
- You could easily swap one listener implementation with another if you need to.
- An
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 code - Since
OnClickListener
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.
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
Button button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do stuff
}
});
OnClickListener is an interface and onClick is method of OnClickListener.
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.
We use
public void button_onClick_name(View v)
{
-------
}
to define a method out of the class. But To define a component Click event within a class, we use onclick listener.
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 for Fragments
, 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
.
We use OnClick
in xml and OnClickListner
in java
code . Both are use to perform a function.
Consider "OnClickListener" as a guy who is waiting your user to click the button of your app. Then your guy will execute your method OnClick().
You have to put an id to your button in your xml file, then give it a name in your MainActivity.java file. Then set a click listener to your guy. And add your onClick method. That's why onClick is bound to the interface View.OnClickListener : https://developer.android.com/reference/android/view/View.OnClickListener.html
Example :
Button myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener(){
@override
public void onClick(View v) {
// your method...
}
}
You can add android:onClick="your_method"
attribute in your XML.
Example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:onClick="your_method"/>
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 useandroid:onClick="your_method"
If you use XML variant, you must implement your_method
in your app class.
everyone has mentioned about OnClickListener
listner which one always used.
i want to add one more point android: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 for OnClickListener
.
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 method findViewById
for Button in that case android:onClick
becomed useful just put that method in activity!!
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.
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 talk 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
.
The main diffrence between onclick()
and setOnClicklisner()
is discribed as follow:
onclick()
Is a attribute in xml. when a button is clickedonclick
method is called suppose you have three button in layout you can add only one function ofonclick()
and when any of one button will clickedonclick()
will calledsetOnClicklistner()
Suppose you have three button in layout you want to perform different action from them. Then you should usesetonClicklistner()
method on each of button to give different method for them
精彩评论