Android Learning (1)-button click Event Response

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.