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;
}
}