java – View. Onclicklistener() function or interface

View. Is onclicklistener() a function or an interface? When we try to set up an onclicklistener () method in Android, we use the new view Onclicklistener(), as far as I know, it annoys me because

>We do not need to initialize objects of classes containing static methods in order to use them Why do we do this? > When we use implements to implement an interface, we do not call the static methods of the interface

Then someone can tell me why:

> new View. Onclicklistener(), used to use onclick() method? > If it is an interface, why should we use view OnClickListener?

Thank you for your reply

Solution

I'm not sure I understand what you're writing about static methods View. Onclicklistener is an interface: http://developer.android.com/reference/android/view/View.OnClickListener.html

To set up a click listener on a view, pass an instance that implements the onclicklisterner interface: http://developer.android.com/reference/android/view/View.html#setOnClickListener (android.view.View.OnClickListener)

The most common way to do this in Android is to define an anonymous inner class that implements onclicklistener( http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html )

myView.setOnClickListener(new View.OnClickListener() {
    @Override           
    public void onClick(View v) {
        // Handle view click here           
    }
});

The above code defines an anonymous inner class and creates its instance It is equivalent to first defining an implementation view Class of onclicklistener (if defined in the same class)

class MyOnClickListener implements View.OnClickListener {
    @Override           
    public void onClick(View v) {
        // Handle view click here           
    }
}

Later I used this

MyOnClickListener listener = new MyOnClickListener();
myView.setOnClickListener(listener);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>