[英]In onClick Event in XML , we give a method name , in which java file does the document have to be in?
Suppose we want to make a button do something , we may use onClick event or onClickListener 假设我们想让按钮做某事,我们可以使用onClick事件或onClickListener
While using onClick Event in XML file , we input a method name. 在XML文件中使用onClick事件时,我们输入一个方法名称。 Where does the method have to be located ? 该方法必须位于何处?
Inside the activity class where the xml layout is inflated. 在活动类中,XML布局被放大。
It has to be public
with this signature: 该签名必须public
:
public void doSomething(View v) {
//..........
}
In the xml of the activity, the attribute set to the Button
must be: 在活动的xml中,设置为Button
的属性必须为:
android:onClick="doSomething"
The v
parameter is actually the View
that triggered this event, v
参数实际上是触发此事件的View
,
in your case a Button
and you can access it by: 在您的情况下为Button
,您可以通过以下方式访问它:
Button button = (Button) v;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.