Click Button Events for Android

Source: Internet
Author: User

For all of us beginners, adding a button to an Android app and adding an event to the back of the button has made it difficult for me to learn two ways to set up a listener and a simple way to add a button click event under the Visual interface. (The following is a description of the program converted to uppercase in lowercase string)

1. Set the Listener:

For a button, first initialize a button, of course, we must first bind its ID, and then initialize an object

< Button         Android:id = "@+id/bnt1"         android:layout_width= "Match_parent"        android:layout_height= "Wrap_ Content "        android:onclick=" Btnconvert_click "        android:text  = "Switch        "/>
Button help= (button) Findviewbyid (R.ID.BHELP);

For the first way, we have done before the Iring project (most certainly not finished, then only set the method of monitoring, feel good trouble! ) as an example:

The Setonclicklistener () function is its click-to-listen function, where the variable is the so-called listener and then adds a function public void onClick (View arg0), Run this function every time you click on the button.

Help.setonclicklistener (new  View.onclicklistener () {                        @Override            publicvoid  OnClick (View arg0) {                //  TODO auto-generated method stub                str= "varies by time, Change the ringtone ";                Display.settext (str);                            }        );

2. The second way, we convert uppercase strings to lowercase strings as an example:

This is the way the teacher teaches us, it is directly in the visual interface of the programming, easy to use. When you add a button to the visual interface, and then in the bottom right of the Properties box, select its View property under Click, and in the corresponding box directly paste your definition of the clicked function, so that when you click, The function that you are going to run is the one that you pasted into, like mine is Btnconvert_click (), then the Btnconvert_click is stuck (without parentheses).

 Public void Btnconvert_click (View v)    {        String s= "";        s=etl.gettext (). toString ();        s=s.touppercase ();        Result.settext ("uppercase string:" +s);    }

So when I click the Convert button, I run the btnconvert_click() function.

Operating effect:

Summarize:

Generally speaking, the two ways can implement button click events, but for our program, it is not the more convenient to feel better, I think the second way (the teacher taught us this kind of) more useful, in their own learning the reason is not understand or cumbersome, because of the method of setting up the listener, messy.

Click Button Events for Android

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.