Android (Interface Programming #6-keyboard operation response)

Source: Internet
Author: User
In Android, operations are performed through the touch screen and keyboard. Program How can we respond to General keyboard and touch pen actions? Through understanding the operations on some basic interface elements of Android, if you are familiar with MVC, you can guess how Android handles Keyboard Events. Congratulations, you guessed it, change the Event Response Function in the activity. It is generally the following three functions:
Onkeydown, onkeyup, on, onkeymultiple
See Code :

Import Android. App. activity;
Import Android. App. progressdialog;
Import Android. OS. Bundle;
Import Android. View. keyevent;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. widget. textview;

Public class testprogress extends activity {

Private progressdialog progress = NULL;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}

@ Override
Public Boolean oncreateoptionsmenu (menu ){
Super. oncreateoptionsmenu (menu );
Menu. Add (0, menu. First + 1, 1, "Open progress ");
Menu. Add (0, menu. First + 2, 2, "exit ");
Return true;
}

@ override
Public Boolean onoptionsitemselected (menuitem item) {
super. onoptionsitemselected (item);
switch (item. getitemid ()
{< br> case menu. first + 1:
{< br> progress = new progressdialog (this);
progress. settitle ("Progress !! ");
progress. setmessage ("Please wait for the operation... ");
progress. setcancelable (true);
progress. show ();
// progress = progressdialog. show (this, "progress! "," Please wait for operation... ");
break;
}< br> case menu. first + 2:
{< br> finish ();
break;
}< BR >}< br> return true;
}

@ Override
Public Boolean onkeydown (INT keycode, keyevent event ){
// Todo auto-generated method stub
Super. onkeydown (keycode, event );
Settitle ("You pressed key:" + String. valueof (keycode ));
Return true;
}

@ Override
Public Boolean onkeymultiple (INT keycode, int repeatcount, keyevent event ){
// Todo auto-generated method stub
Super. onkeymultiple (keycode, repeatcount, event );
Textview TV = (textview) This. findviewbyid (R. Id. mainview );
TV. settext ("You have press Key: [" + String. valueof (keycode) + "] for:" + String. valueof (repeatcount) + "times! ");
Return true;
}

@ Override
Public Boolean onkeyup (INT keycode, keyevent event ){
// Todo auto-generated method stub
Super. onkeyup (keycode, event );
Settitle ("You release key:" + String. valueof (keycode ));
Return true;
}
}

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.