First, the OnClick event
button and ImageButton have an onclick event that adds a click event through their own. Setonclicklistener (Onclicklistener) method
All controls have an OnClick event, not just buttons and ImageButton.
What happens after clicking a button by tapping the event's listener
Second, the basic steps of the operation:
The first step: Declare a private variable to store the control object you get to
Step Two: Initialize the currently needed control (find the control that needs to be manipulated)
Step three: Set the listener of the button to implement what we click button to do
Third, anonymous internal class way to implement the listener onclick event
PackageCom.muke.textview_edittext;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {PrivateButton Loginbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //1. Initialize the currently required control (that is, find the control that needs to be manipulated, the Findviewbyid method returns the View object and needs to be transformed down into a button object)Loginbutton =(Button) Findviewbyid (R.id.button1); //2, Set button listener, through the listener to achieve our click button to operate the thingLoginbutton.setonclicklistener (NewOnclicklistener () {@Override//listen for the action of clicking button in the current OnClick method Public voidOnClick (View v) {System.out.println ("My button was clicked."); } }); }}
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/login" /></LinearLayout>
Four, external class mode to implement the listener onclick event
PackageCom.muke.textview_edittext;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateButton Loginbutton; PrivateButton Registerbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //1. Initialize the currently required control (that is, find the control that needs to be manipulated, the Findviewbyid method returns the View object and needs to be transformed down into a button object)Loginbutton =(Button) Findviewbyid (R.id.button1); Registerbutton=(Button) Findviewbyid (R.id.button2); //2, Set button listener, through the listener to achieve our click button to operate the thingLoginbutton.setonclicklistener (NewMyonclicklistener () { Public voidOnClick (View v) {Super. OnClick (v);//Myonclicklistener is the parent class that executes this method when the button is clicked and executes the OnClick method of the parent class.Toast.maketext (mainactivity. This, "Loginbutton to execute (subclass)", 1). Show (); } }); Registerbutton.setonclicklistener (NewMyonclicklistener () { Public voidOnClick (View v) {Super. OnClick (v);//Myonclicklistener is the parent class that executes this method when the button is clicked and executes the OnClick method of the parent class.Toast.maketext (mainactivity. This, "Registerbutton to execute (subclass)", 1). Show (); } }); }}//Onclicklistener is an interface, so use implements to implementclassMyonclicklistenerImplementsonclicklistener{@Override Public voidOnClick (View v) {System.out.println ("Loginbutton and Registerbutton to execute (parent class)"); V.setalpha (0.5F);//set two buttons Loginbutton and Registerbutton transparency } }
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/login" /> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/register" /></LinearLayout>
Click events for Android Monitor button and ImageButton controls