The first and most commonly used form is to fire events that should be handled when a button is clicked by adding a listener to the onclick event. Such as:
Btn1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, "+") by adding a listener for the onclick () event ", Toast.length_long). Show ();});
Second form: A method for adding a response to the android:onclick=" property of a component at the layout interface.
Layout file:
<button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:o nclick= "Submit" android:text= "by adding a response method to the Android:onclick property"/>
Class Code:
/** * This method calls in the layout file * */public void Submit (view view) {Toast.maketext (mainactivity.this, "+" via for Android:o The Nclick property adds the way the response method ", Toast.length_long). Show ();
Tips: the second form of the method must be declared as public, and the parameter must be of the view type, otherwise the layout file's android:onclick= "" property will not be able to find the specified method.
Program run: