[Basic learning for Android]-event monitoring and triggering

Source: Internet
Author: User

I have been preparing to start learning Android before, but it is always a matter of delaying the planning. Recently I decided to start learning this course with a hard heart.

Disclaimer: The book "Secrets of Android Application Development", which records the logs of the book, references the relevant code and summary, and has no commercial use, it is completely a record of self-learning, and many problems will inevitably occur in learning just now. If there are any mistakes, please criticize them a lot.

The environment can be set up on the Internet. Currently, the development environment jdk6.0 + eclipse3.6 + Android SDK 2.2 is available, because your mobile phone is version 2.2, at the beginning, I did not select the latest version.

 

1. General Project steps:
1. Create a secret UI, that is, the Res/layout/Main. xml file.
2. Assist in the first step to write the related constant Res/values/string. xml file.
3. Write related activities

2. control events listen to and process events by setting the listener of the control
Press button event: rewrite the onkeydown Method
Press the button to pop up the event: rewrite the onkeyup Method
Click Event: ontouchevent Method

Toast control: Toast. maketext (this, String, Toast. length_short). Show (); display prompt information

Iii. Important code explanation
// Obtain the Button Object
Button button_ OK = (button) This. findviewbyid (R. Id. OK );
In activity, get the corresponding button under the current instance. In this instance, the index ID automatically created corresponds to the path under R. Java in the gen directory.

(1) event monitoring and triggering of controls
In Android, the control time is listened to and rewritten by setting the listener of its control.
// Set the button control listener
Button_ OK .setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
// Todo auto-generated method stub
// Process the event here

}
});

(2). It is generally used to trigger events. Such events are triggered by listening to the event during the entire activity instance operation. You can simply overwrite the event.
These common event processing methods, each key corresponds to a key value, you can change the key function as needed, so you need to build your own keyevent object (refer to (3 ))

Public Boolean onkeyup (INT keycode, keyevent event ){
// Todo auto-generated method stub
Switch (keycode)
{
Case keyevent. keycode_dpad_center:
Displaytoast ("pop-up: Middle ");
Break;
Case keyevent. keycode_dpad_up:
Displaytoast ("Bounce: up arrow key ");
Break;
Case keyevent. keycode_dpad_down:
Displaytoast (": ");
Break;
Case keyevent. keycode_dpad_left:
Displaytoast ("Bounce: left arrow key ");
Break;
Case keyevent. keycode_dpad_right:
Displaytoast ("Bounce: right arrow key ");
Break;
}
Return super. onkeyup (keycode, event );
}

/* Trigger event */
Public Boolean ontouchevent (motionevent event ){
// Todo auto-generated method stub
Int iaction = event. getaction ();
If (iaction = motionevent. action_cancel |
Iaction = motionevent. action_down |
Iaction = motionevent. action_move)
{
Return false;
}
// Obtain the position of the PEN click
Int x = (INT) event. getx ();
Int y = (INT) event. Gety ();

Displaytoast ("Touch coordinate: (" + integer. tostring (x) + "," + integer. tostring (y) + ")");

Return super. ontouchevent (event );
}

(3) construct the keyevent object by yourself
Public Boolean onkeydown (INT keycode, keyevent event)
{
// Construct the keyevent object. Its function is the return key function.
// Press any key to execute the return key function.
Keyevent key = new keyevent (keyevent. action_down, keyevent. keycode_back );

// The input parameter is the key of the self-built keyevent object.
Return super. onkeydown (key. getkeycode (), key );
}

 

Go to P60.

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.