Android Monitor Return key

Source: Internet
Author: User

In Android development, the monitoring of Return key events is often used, following an example to demonstrate the Android to the return key event monitoring.

public class Backkeytest extends activity{/** called when the Activity is first created. */@Overridepublic void onCreate (Bundle Savedin Stancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {// Create Exit Dialog Alertdialog Isexit = new Alertdialog.builder (this). Create ();//Set Dialog title Isexit.settitle ("System tip");// Set the dialog message Isexit.setmessage ("OK to exit"),///Add Select button and register Listener Isexit.setbutton ("OK", listener); Isexit.setbutton2 ("Cancel", listener);//Display dialog box Isexit.show ();} return false;} /** Listener dialog box button click event */dialoginterface.onclicklistener listener = new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int which) {switch (which) {case alertdialog.button_positive://"Confirm" button Exit program finish (); Break;case alertdialog.button_negative://"Cancel" the second button cancels the dialog box Break;default:break;}};} 

Android phone commonly used three keys, home key, back key and menu key.
In the application we often do a certain amount of processing, convenient for users to use.
First we need to know exactly what the system did when we clicked three keys.
If no listening process is performed,
When you click the Home key, the system only executes the OnStop () method of the activity currently displayed by the application and then jumps out of the interface.
When you click the back key, the system defaults to the finish () method of the application's current activity and then jumps out of the interface.
When you click the menu key, the system does not do any processing by default.


Here is just a simple application demo, we can design a more perfect Exit Program dialog box according to our own needs.


Other processing can also be done after the supervisor hears the return event, and so on.

Android Monitor Return key

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.