Java 中典型的“onClick”调用中的“View v”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32362092/
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
What is "View v" in the typical "onClick" call in Java?
提问by DSlomer64
In the generated code below, v
is never referred to. What exactly does it represent in terms of the clicking and listening and subsequent action that occurs?
在下面生成的代码中,v
从未提及。就点击和聆听以及随后发生的动作而言,它究竟代表什么?
btnReturnToUI.setOnClickListener
(new OnClickListener()
{
public void onClick(View v) /////////////////////////////
{
MainActivity currentActivity = (MainActivity) getActivity();
currentActivity.showUI();
}
}
);
回答by zapl
It's the thing you've clicked / set the listener on. In your example it's the same as btnReturnToUI
.
这是你点击/设置监听器的东西。在您的示例中,它与btnReturnToUI
.
One of the uses for this is to register the same listener on multiple buttons and then do different things in the listener depending on v
or v.getId()
which is the id in the xml layout e.g. @+id/the_id
其用途之一是在多个按钮上注册相同的侦听器,然后根据xml 布局中的 idv
或v.getId()
哪个是 id 在侦听器中执行不同的操作,例如@+id/the_id
回答by M Anouti
I guess this is in Android context. v
is the view that was clicked (it could be a Button
, a TextView
, etc.). See the API documentationfor a description:
我想这是在 Android 上下文中。v
是被点击的视图(它可以是 a Button
、 aTextView
等)。有关说明,请参阅API 文档:
v
The view that was clicked.
v
单击的视图。
回答by Tharaka Devinda
It is the view that was clicked. That parameter is there so that you can assign a single OnClickListener to many views and still find out which view was clicked.
这是被点击的视图。该参数在那里,以便您可以将单个 OnClickListener 分配给许多视图,并且仍然可以找出单击了哪个视图。
You can separate them from an ID or even the type of View so that you can perform different actions depending on the type or ID of the clicked item.
您可以将它们与 ID 甚至 View 的类型分开,以便您可以根据所单击项目的类型或 ID 执行不同的操作。