Button controls in Android

Source: Internet
Author: User
Tags stub xmlns

The button control in Android should count as a simpler control, however, it's used very high, and today I've summed up three common ways to achieve its function by clicking button.

1. Most of the time, when we use the button control, it is often "one-time" use, at this time, for the sake of convenience, we generally use the anonymous inner class method, like this:

Button1.setonclicklistener (New Onclicklistener () {  
                  
            @Override public
            void OnClick (View v) {  
                //TODO auto-generated method Stub  
                System.out.println ("You clicked Button1");  
            }  
        );

As we can see, such code is not only short, but also clear and understandable, but this method generally applies only if the button is not used more often or "one-time" use

2. When the button has more than one or the use of a lot of button, we need to use the binding listener, in fact, the binding listener there are several methods, but I do not enumerate here, after all, those methods in the actual application is also not common.

Our general approach is to implement the Onclicklistener interface and implement one of the methods, just like this:

@Override public
    void OnClick (View v) {  
        //TODO auto-generated a stub  
        switch (V.getid ()) {case  
        R. Id.button2:  
            System.out.println ("You clicked on Button2");  
            break;  
      
        Default: Break  
            ;  
        }  
    }

Note: The OnClick method is a method in the Onclicklisten interface, and we must implement it by implementing this interface.

3. This is one of the easiest ways to do what we need to do is add a method and add a property to the button:

<button
        android:id= "@+id/button3"
        android:layout_width= "Match_parent"
        Wrap_content "
        android:text=" Button3 test "
        android:onclick=" ClickHandler "
        />"

Among them, we added the onclick attribute more than usual.

So, we need to add the methods we declare in our code:

public void ClickHandler (view view) {  
        System.out.println ("You clicked Button3");  
    }

Finally, paste out the complete source code and the realization effect screenshot:

1. layout file

 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android xmlns:tools=" http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:cont" Ext= ". Mainactivity "android:orientation=" vertical "> <textview android:layout_width=" Wrap_con Tent "android:layout_height=" Wrap_content "android:text=" @string/hello_world "/> <b Utton android:id= "@+id/button1" android:layout_width= match_parent "android:layout_height=" Wrap_co Ntent "android:text=" Button1 test "/> <button android:id=" @+id/button2 "Android  
      : layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "Button2 test"/> <button android:id= "@+id/button3" android:layout_width= "Match_parent" android:layout_he ight= "Wrap_content" Android:text= "Button3 test" android:onclick= "ClickHandler"/> </linearlayout> 

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.