Android event handling mechanism, toast control

Source: Internet
Author: User

 Packagecom.lxt008;ImportCOM.LXT008.R;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.KeyEvent;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast;/*** * Control events listen and handle events by setting the listener of their controls * key press events: By overriding the OnKeyDown method * Key Bounce event: By overriding the OnKeyUp method * Stylus Click event: By implementing the Ontouchevent method * The example uses TOAS T control: * Toast.maketext (this, string, Toast.length_short). Show (); * Display message **/ Public classActivity01extendsactivity{ Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //Get Button ObjectButton BUTTON_OK =(Button) Findviewbyid (R.id.ok); //Setting the button control listenerButton_ok.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {//Handling Events hereDisplaytoast ("Click OK button");    }        }); }    /*Press the button to trigger the event*/     Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        Switch(keycode) { CaseKeyEvent.KEYCODE_DPAD_CENTER:DisplayToast ("Press: Middle button");  Break;  CaseKeyEvent.KEYCODE_DPAD_UP:DisplayToast ("Press: Up ARROW key");  Break;  CaseKeyEvent.KEYCODE_DPAD_DOWN:DisplayToast ("Press: DOWN ARROW key");  Break;  CaseKeyEvent.KEYCODE_DPAD_LEFT:DisplayToast ("Press: Left ARROW key");  Break;  CaseKeyEvent.KEYCODE_DPAD_RIGHT:DisplayToast ("Press: Right ARROW key");  Break; }        return Super. OnKeyDown (KeyCode, event); }    /*the event triggered by the button bouncing*/     Public BooleanOnKeyUp (intKeyCode, KeyEvent event) {        Switch(keycode) { CaseKeyEvent.KEYCODE_DPAD_CENTER:DisplayToast ("Pop Up: Middle button");  Break;  CaseKeyEvent.KEYCODE_DPAD_UP:DisplayToast ("Bounce: Up ARROW key");  Break;  CaseKeyEvent.KEYCODE_DPAD_DOWN:DisplayToast ("Bounce: down ARROW key");  Break;  CaseKeyEvent.KEYCODE_DPAD_LEFT:DisplayToast ("Bounce: Left ARROW key");  Break;  CaseKeyEvent.KEYCODE_DPAD_RIGHT:DisplayToast ("Bounce: Right ARROW key");  Break; }                return Super. OnKeyUp (KeyCode, event); }         Public BooleanOnkeymultiple (intKeyCode,intRepeatCount, KeyEvent event) {                return Super. Onkeymultiple (KeyCode, RepeatCount, event); }        /*Stylus Events*/     Public Booleanontouchevent (Motionevent event) {intIAction =event.getaction (); if(iaction = = Motionevent.action_cancel | |iaction= = Motionevent.action_down | |iaction==motionevent.action_move) {            return false; }        //where to get a stylus click        intx = (int) Event.getx (); inty = (int) event.gety (); Displaytoast ("Stylus Click Coordinates: (" +integer.tostring (x) + "," +integer.tostring (y) + ")"); return Super. Ontouchevent (event); }        /*Show Toast*/     Public voiddisplaytoast (String str) {Toast.maketext ( This, str, toast.length_short). Show (); }}

Layout code

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    >    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello"    />    <ButtonAndroid:id= "@+id/ok"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "OK"/></LinearLayout>

Android event handling mechanism, toast control

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.