The most common click events are three ways to create a setonclicklistener (dynamic run) in the Mainactivity.java oncreate function (the program that initiates the program is the first to run) (the most common)
protected void OnCreate (Bundle savedinstancestate) {
Button button= (button) Findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {
@Override Me
public void OnClick (View v) {
What to do when you click the event Monitor
}
});
}
button is one of the buttons on my design.
In addition to the dynamic operation, there are configuration methods, but only for the OnClick event configuration method, configuration method needs to add a property to the control on the design
android:onclick= "Test"//test is the name of the event I created, and this event will be declared in Mainactivity.java
public void Test (view view)
//view refers to a clicked control and cannot be modified
//For example, to modify the XY axis of a button, here is not write Button.setx () and buttonsety (), but View.setx () and View.sety ()
See the second way to learn JS classmates feel very familiar, because and JS on the way the event implementation is the same.
The third Way is reuse, which is just fine-tuned in the original code, and in the second way, he creates a function body
private View.onclicklistener mylistener=new View.onclicklistener () {
@Override
public void OnClick (View v) {
}
};
Button.setonclicklistener (MyListener);
}
It seems that the third and the first are not much different, but the third one offers many conveniences when many controls need to be clicked, comparing three click events
Dynamic operation is convenient in the small program, and easy to understand, and many controls need to implement the same event, the use of reuse, in the event of judgment, which can improve the readability, and configuration is only applicable to click events, when the event is clicked, configuration method is also convenient, and easy to understand.
Android Studio Learning Essays-Basic events (click)