[Android] 2, using the 2 method to do button monitoring and image button use

Source: Internet
Author: User

The first approach is to use a click Listener (the commented out part of the code): This method binds the button to the click Listener (23,24) Btn_ok.setonclicklistener (this) in the initialized function. the processing is then uniformly written in the abstract function onclick (View v), and the V = = BTN_OK to determine which button is clicked. (28~34)

The second method is to use the inner class to implement the key listening, as follows (this looks like a little more code, the individual processing is separate)

1  PackageCom.himi.button;//Package Path2 //Import imported Class library3 4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 ImportAndroid.widget.TextView;Ten  One /*listening using the TAP Listener interface A Public class Mainactivity extends Activity implements Onclicklistener {//Use Click Listener - private Button Btn_ok, Btn_cancel; - private TextView TV; the      - @Override - Public void OnCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); + Setcontentview (r.layout.main); - BTN_OK = (Button) Findviewbyid (R.ID.BTN_OK); + Btn_cancel = (Button) Findviewbyid (r.id.btn_cancel); A TV = (TextView) Findviewbyid (r.id.tv); at Btn_ok.setonclicklistener (this);//Bind the BTN_OK button to the tap listener - Btn_cancel.setonclicklistener (this);//Bind the Btn_cancel button to the tap listener -      } -      - @Override - Public void OnClick (View v) {//Use listener to rewrite its abstract function in if (v = = BTN_OK) { - tv.settext ("OK button trigger event! "); to } else if (v = = btn_cancel) { + tv.settext ("Cancel button trigger event! "); -          } the     } *  } $  */Panax Notoginseng //internal class implements key monitoring -  Public classMainactivityextendsActivity {//use a click Listener the     PrivateButton Btn_ok, Btn_cancel; +     PrivateTextView TV; A  the @Override +      Public voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); $ Setcontentview (r.layout.main); $BTN_OK =(Button) Findviewbyid (R.ID.BTN_OK); -Btn_cancel =(Button) Findviewbyid (r.id.btn_cancel); -TV =(TextView) Findviewbyid (r.id.tv); theBtn_ok.setonclicklistener (NewOnclicklistener () {//bind the Btn_ok button on the tap listener - @OverrideWuyi              Public voidOnClick (View v) { theTv.settext ("OK button trigger event! "); -             } Wu         }); -Btn_cancel.setonclicklistener (NewOnclicklistener () {//bind the Btn_cancel button on the tap listener About @Override $              Public voidOnClick (View v) { -Tv.settext ("Cancel button trigger event! "); -             } -         }); A     } +  the}

Understand the image below the button is very simple: the 9th line below is visible to me here is not a mouse click monitoring, but touch screen monitoring. This effect is achieved by changing the background of the ImageButton based on the touch screen event. The picture resources here are saved by calling Getresources (). getdrawable (r.drawable.xxx) to get the resources.

1  Public classMainactivityextendsActivity {2     PrivateImageButton ibtn;3 @Override4      Public voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.main);7IBTN =(ImageButton) Findviewbyid (R.ID.IMAGEBTN);8         //Add touch screen monitoring for picture buttons9Ibtn.setontouchlistener (NewOntouchlistener () {Ten @Override One              Public BooleanOnTouch (View V, motionevent event) { A                 //the user is currently pressing the -                 if(event.getaction () = =Motionevent.action_down) { -                     //Set Picture button background image the ibtn.setbackgrounddrawable (Getresources (). getdrawable (r.drawable.press)); -                 //the user is currently lifting -}Else if(event.getaction () = =motionevent.action_up) { - ibtn.setbackgrounddrawable (Getresources (). getdrawable (r.drawable.nopress)); +                 } -                 return false; +             } A         }); at     } -}

Note : The button here is imagebutton!

This article link: http://www.cnblogs.com/zjutlitao/p/4229564.html

More Highlights: Http://www.cnblogs.com/zjutlitao

[Android] 2, using the 2 method to do button monitoring and image button use

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.