简体   繁体   中英

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

While using onClick Event in XML file , we input a method name. Where does the method have to be located ?

Inside the activity class where the xml layout is inflated.
It has to be public with this signature:

public void doSomething(View v) {
  //..........
}

In the xml of the activity, the attribute set to the Button must be:

android:onClick="doSomething"

The v parameter is actually the View that triggered this event,
in your case a Button and you can access it by:

Button button = (Button) v;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM