With eclipse, you can add a button in your own application, and the following code is automatically added in Main. xml:
---
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />
When you compile and run the program, a button is displayed. When you click it, there is no response or action.
We need to manually add code to add a click event response for this button.
There are two ways to add a response to a click event for a button:
1. Bind a click event listener to the button through the setonclicklistener method of the button to listen to the user's click events. The Code is as follows:
Public class myactivity extends activity {protected void oncreate (bundle icicle) {super. oncreate (icicle); setcontentview (R. layout. content_layout_id); // manually add the code to start. // bind the button to an event listener for a single operation. Used to listen for user click operations. Final button = (button) findviewbyid (R. id. button_id); button. setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// perform action on click // Add your own code ...... final textview text = (textview) findviewbyid (R. id. textview1); text. settext ("onclick. "+ ".... ") ;}}); // manually add code to end }}
The above code is very short, but it is not easy to read. You can also use the following writing method:
Public class myactivity extends activity {protected void oncreate (bundle icicle) {super. oncreate (icicle); setcontentview (R. layout. content_layout_id); // manually add the code to start. Button button = (button) findviewbyid (R. id. button_id); button. setonclicklistener (myonclicklistener); // manually add code to end} // manually add code to start private onclicklistener myonclicklistener = new onclicklistener () {public void onclick (view V) {// Add your own code ...... final textview text = (textview) findviewbyid (R. id. textview1); text. settext ("onclick. "+ ".... ") ;}}; // manually add code to end}
2. Add a click event request to the button by modifying the button attribute in Main. xml. The modified XML file is as follows:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="OnMySelfClick" android:text="Button" />
Then add the onmyselfclick function mentioned in the XML file to the. Java file:
Public class helloandroidactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main );} /************************************ button click operation handler. * The following function is a processing function clicked by a button. * It needs to add an onclick attribute to the button in layout XML, * and specify its processing function. As shown in the following ** Android: onclick = "onmyselfclick" **, you do not need to add the complex code above. * **/Public void onmyselfclick (view v) {final textview text = (textview) findviewbyid (R. id. textview1); text. settext ("onmyselfclick. "+ ".... ");}}
The second method is much simpler than the first one.