View.OnClickListener,方法还是类? [英] View.OnClickListener, method or class?
问题描述
我知道View是一个很好的课程..但我不知道我不明白View.OnClickListener()是否是一种方法。
我怀疑它除非它返回一个对象?
我认为View是一个具有静态OnClickListener成员对象的类,对我来说没有意义..
有些人可以解释一下这发生了什么代码行?
button1 =(Button)findByView(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
}
}
那么这段代码会发生什么?
Button1
是对xml文件中button1对象的引用。
button1
object有一个成员对象 setOnClickListener
我从它的名字我假设将一个事件初始化为按钮或其他东西。但是它收到 View.OnClicListener()
object。
我对此感到困惑.onClick收到一个View对象所以 onClickListener
不是一个对象返回一个View对象??我根本没有得到它。
有人可以解释该行中发生的事情 View.onClickListener()
这是另一种说法 new this
?
View.OnClickListener
是一个界面,您不会调用它,但会创建一个新实例( new View.OnClickListener()
是对构造函数的调用)
您创建的实例是匿名类 实现
查看。 OnClickListener
,在右下方的括号中新建View.OnClickListener()
任何类实现 View.OnClickListener
必须实现其中声明的方法(例如 onClick )
setOnClickListener
只保存对您提供的View.OnClickListener实例的引用,当有人单击该按钮时,你设置的监听器的onClick
方法被调用。
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?
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
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.
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.
Can someone explain what happens in that line View.onClickListener()
is it another way of saying new this
?
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)
The instance you create is of anonymous class that implements
View.OnClickListener
, in the brackets right under new View.OnClickListener()
Any class that implements View.OnClickListener
must implement the methods declared in it (e.g. 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.
这篇关于View.OnClickListener,方法还是类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!