java View.OnClickListener,方法还是类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6946971/
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
View.OnClickListener, method or class?
提问by Lews Therin
sorry if this question is stupid, but I can't wrap my head around Java syntax..I learnt C/C++
I know View is a class which is good..but I don't understand if View.OnClickListener() is a method.
I doubt it unless it returns an object?
I think View is a class which has a static OnClickListener member object..again that doesn't make sense to me..
Can some explain what is happening with this line of code?
对不起,如果这个问题很愚蠢,但我无法理解 Java 语法。我学习了 C/C++
我知道 View 是一个很好的类......但我不明白 View.OnClickListener() 是否是一个方法。
我怀疑它,除非它返回一个对象?
我认为 View 是一个具有静态 OnClickListener 成员对象的类..这对我来说又没有意义..
有人可以解释这行代码发生了什么吗?
button1 = (Button) findByView(R.id.button1) ;
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
}
}
So what is happening with this code?
那么这段代码发生了什么?
Button1
is a reference to the button1 object in the xml file.
Button1
是对 xml 文件中 button1 对象的引用。
button1
object has a member object setOnClickListener
which I from its name I assume initializes an event to the button or something. But it receives View.OnClicListener()
object.
button1
object 有一个成员对象setOnClickListener
,我从它的名字中我假设它初始化了一个按钮或其他东西的事件。但它接收View.OnClicListener()
对象。
I am confused by that..onClick receives a View object so onClickListener
is not an object returns a View object?? I don't get it at all.
我很困惑..onClick 接收到一个 View 对象,所以onClickListener
不是一个对象返回一个 View 对象吗??我完全不明白。
Can someone explain what happens in that line View.onClickListener()
is it another way of saying new this
?
有人可以解释在那条线上发生的事情View.onClickListener()
是另一种说法new this
吗?
回答by MByD
View.OnClickListener
is an interface, you don't call it, but creates a new instance of it (new View.OnClickListener()
is a call to the constructor)
View.OnClickListener
是一个接口,你不调用它,而是创建它的一个新实例(new View.OnClickListener()
是对构造函数的调用)
The instance you create is of anonymous classthat implements
View.OnClickListener
, in the brackets right under new View.OnClickListener()
您创建的实例是匿名类是implements
View.OnClickListener
,在括号右下new View.OnClickListener()
Any class that implements View.OnClickListener
must implement the methods declared in it (e.g. onClick)
任何View.OnClickListener
实现的类都必须实现其中声明的方法(例如onClick)
setOnClickListener
just saves the reference to the View.OnClickListener instance you supplied, and when someone clicks the button, the onClick
method of the listener you set is getting called.
setOnClickListener
只需保存对您提供的 View.OnClickListener 实例的引用,当有人单击按钮时,onClick
您设置的侦听器的方法就会被调用。
回答by Woody
OnClickListener is an interface. An interface provides a set of Methods other classes can implement. http://download.oracle.com/javase/tutorial/java/concepts/interface.html
OnClickListener 是一个接口。一个接口提供了一组其他类可以实现的方法。http://download.oracle.com/javase/tutorial/java/concepts/interface.html
You could have another class (Like and adapter), that extends OnClickListener, then your Adapter class could add the method "OnClick(View v)", and it would also be able to handle Click events. Or you could use the code you posted, where you just create an anonymous class, that implements OnClickListener.
您可以有另一个类(Like 和适配器),它扩展 OnClickListener,然后您的 Adapter 类可以添加方法“OnClick(View v)”,并且它还能够处理 Click 事件。或者您可以使用您发布的代码,您只需在其中创建一个实现 OnClickListener 的匿名类。
-Woody
-伍迪
回答by Noah
Android code is geared for event based responses. The block of code is as follows:
Android 代码适用于基于事件的响应。代码块如下:
Find a button that you've added to the active layout, and assign it to a local variable:
找到您已添加到活动布局的按钮,并将其分配给局部变量:
button1 = (Button) findByView(R.id.button1);
Set the on click listener for the button. This is a class that will be invoked when the button registers an event. The class is constructed here, it is anonymous as you don't assign it to a variable, but android will keep track of the reference.
设置按钮的点击监听器。这是一个将在按钮注册事件时调用的类。该类是在此处构造的,它是匿名的,因为您没有将其分配给变量,但 android 会跟踪引用。
Button events are always due to being pressed, so when the button registers that it has been pressed, it will inform the onClickListener class that the even occurred, and pass itself in as the view. The onClickListener is constructed as:
按钮事件总是由于被按下,所以当按钮注册它已被按下时,它会通知 onClickListener 类事件发生,并将自身作为视图传入。onClickListener 构造为:
new View.OnClickListener()
{
public void onClick(View v)
{
}
}
That onClick method is used by the listener to handle the event (in this case, a button press). So, you would put the code you would like executed in that method.
侦听器使用该 onClick 方法来处理事件(在本例中为按下按钮)。因此,您可以将想要执行的代码放入该方法中。
To answer your question directly, the onClickListere is an anonymous class that defines the onClick method, which will handle button events.
为了直接回答您的问题,onClickListere 是一个匿名类,它定义了 onClick 方法,该方法将处理按钮事件。