简体   繁体   English

在XML中的onClick事件中,我们提供了一个方法名称,文档必须位于哪个Java文件中?

[英]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.

相关问题 如果我们在Java中有重写的方法,哪个方法称为第一个方法? - Which method is called the first one if we have overriden methods in Java? 我们应该在Java中使用方法名后面有换行符吗? - Should we have a newline after method name in Java? 如果我们有任何方法可以从 html 中运行带有来自外部 html 页面的 onclick 事件的函数? - if any method we have from html to run a function with onclick event from external html page? 如果我们有多个XML配置文件,DispatcherServlet如何工作? - How does DispatcherServlet work if we have multiple XML Configuration file? 用Java将XML文档写入文件的最佳方法是哪种? - Which is the best way to write a XML Document to a file in java? 重写XML文件时使用哪种Java XML Parsing方法? - Which Java XML Parsing method to use when rewriting an XML file? 为什么我们必须在onClick方法中添加“视图”作为参数及其作用? - Why do we have to add 'View' as parameter in onClick method and what it does? onclick 事件不适用于 cardview? Java xml - onclick event not working on cardview? Java xml 为什么Activity没有onClick事件处理程序? - Why does Activity not have a onClick event handler? Java Integer 类是否有返回 Comparator 的比较方法? - Does Java Integer class have a compare method which returns Comparator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM