Android development path (7) responding to Keyboard Events

Source: Internet
Author: User

Reprinted please indicate from "LIU Da's csdn blog": http://blog.csdn.net/poechant

When using an Android Application, you must perform your own operations to make the android program respond accordingly. This method can be implemented through Keyboard Events, touch events, sensor events, and so on. The following describes how to respond to Keyboard Events.

Keyboard Events include onkeydown, onkeyup, onkeymultiple, and onkeylongpress. We only need to override these methods in the class that inherits the activity we implement. First, we use the display prompt of the maketext method class of the toast class as the response of the key. First, we create a subclass of the activity as we did before:

Public class test
Extends activity {

Public void oncreate (bundle savedinstancestate ){

...

}

Public Boolean onkeydown (INT keycode, keyevent event ){

...

}

Public Boolean onkeyup (INT keycode, keyevent event ){

...

}

Public Boolean onkeymultiple (INT keycode, keyevent event ){

...

}

Public Boolean onkeylongpress (INT keycode, keyevent event ){

...

}

Public void displaytoast (string MSG ){

Toast. maketext (this, MSG, Toast. length_short). Show ();

}

}

The first parameter of the response method for the four key events is the key for the value, for example, whether you press a, B, c, or 1, 2, or 3, or space, up, down, and so on, while the second parameter is the corresponding event. Add a button in oncreate to set the layout. The following describes how to override the onkeydown method. Displaytoast is to wrap the toast. maketext method.
The use of the toast class will be introduced in future blog posts.

Public Boolean onkeydown (INT keycode, keyevent event ){

Switch (keycode ){

Case keyevent. keycode_dpad_center:

Displaytoast ("Press \" center \ "key ");

Case keyevent. keycode_dpad_up:

Displaytoast ("Press \" up \ "key ");

Case keyevent. keycode_dpad_down:

Displaytoast ("Press \" down \ "key ");

Default:

Displaytoast ("illegal key has been pressed ");

}

Return super. onkeydown (keycode, event );

}

The keyevent class contains static constant members corresponding to various buttons. For example, keycode_dpad_center is the middle key on the keyboard, keycode_dpad_up is the upward key on the keyboard, and keycode_dpad_down is the downward key on the keyboard. The implementation methods of other onkeyup, onkeymultiple, and onkeylongpress methods are similar to them, that is, matching the corresponding buttons and then specifying the required operation.

You can also build your own keyevent by using the following methods:

Public keyevent (INT action,
Int Code );

Public keyevent (long downtime,
Long eventtime, int action,
Int code, int repeat );

Public keyevent (long downtime,
Long eventtime, int action,
Int code, int repeat,
Int metastate );

Public keyevent (long downtime,
Long eventtime, int action,
Int code, int repeat,
Int metastate, int device,
Int scancode );

Public keyevent (long downtime,
Long eventtime, int action,
Int code, int repeat,
Int metastate, int device,
Int scancode, int flags );

Public keyevent (long time, string characters,
Int device, int flags );

Public keyevent (keyevent origevent );

Public keyevent (keyevent origevent,
Long eventtime, int newrepeat );

Enjoy Android :)

Reprinted please indicate from "LIU Da's csdn blog": http://blog.csdn.net/poechant

-

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.