eclipse View.OnClickListener() 一个函数或接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11815101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:45:48  来源:igfitidea点击:

View.OnClickListener() a function or Interface

javaandroideclipse

提问by Alfred James

Is View.OnClickListener() a function or interface? When we try to set a onclicklistener() method in android, we use new View.OnClickListener() and it bugs me there cuz as far as I know,

View.OnClickListener() 是函数还是接口?当我们尝试在 android 中设置 onclicklistener() 方法时,我们使用了 new View.OnClickListener() 并且据我所知它在那里让我感到烦恼,

  • we don't need to initialize an object of class containing static method inorder to use those methods. Why we do this?
  • When we use implements inorder to implement an interface, we don't call the static methods of the interface.
  • 我们不需要初始化包含静态方法的类的对象来使用这些方法。我们为什么要这样做?
  • 当我们使用implements来实现接口时,我们不会调用接口的静态方法。

So can some one tell me why do we do:

那么有人可以告诉我我们为什么要这样做:

  • new View.OnClickListener(), for using onclick() method?
  • Why do we use () with View.OnClickListener if it is an interface?
  • new View.OnClickListener(),用于使用 onclick() 方法?
  • 如果它是一个接口,为什么我们将 () 与 View.OnClickListener 一起使用?

Thanks for your reply..

感谢您的回复..

回答by toucan

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

我不确定我是否理解您所写的关于静态方法的内容。View.OnClickListener 是一个接口:http: //developer.android.com/reference/android/view/View.OnClickListener.html

To set a click listener on a view, you pass an instanceimplementing the OnClickListerner interface: http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)

要在视图上设置单击侦听器,请传递一个实现 OnClickListerner 接口的实例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 (http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html) that implements OnClickListener like

在 android 中最常见的方法是定义一个匿名内部类(http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html),它实现了 OnClickListener 之类的

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

The code above both defines an anonymous inner class and creates an instance of it. It is equivalent to first defining a class implementing View.OnClickListener (if defined in the same class)

上面的代码定义了一个匿名内部类并创建了它的一个实例。相当于先定义一个实现View.OnClickListener的类(如果定义在同一个类中)

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

And later using this

后来使用这个

MyOnClickListener listener = new MyOnClickListener();
myView.setOnClickListener(listener);

回答by VJ Vishal Jogiya

enter image description here

在此处输入图片说明

Sample Code,

示例代码,

Internally it works something like this,

在内部它的工作原理是这样的,

public class MyView{

public stinterface MyInterface{
        public void myOnClick(View view);
    }

}

 public class MyButton{
        View view;

        public void setOnClicker(MyInterface onClicker) {
            onClicker.myOnClick(view);
        }
    }


public class MyExample{

    public void method(){
        MyButton myButton = new MyButton();
        myButton.setOnClicker(new MyInterface() {
            @Override
            public void myOnClick(View view) {

            }
        });
    }
}