Go How Android listens for a return key, pops up an exit dialog box

Source: Internet
Author: User

This article transferred from: http://blog.csdn.net/sunnyfans/article/details/8094349

How Android listens for the return key click event and creates an exit dialog box,

Prevent yourself from writing an application that accidentally clicks the exit key and exits directly. Keep a record of this simple demo, spare.

Note: The following code was copied from the Internet, and now forget which the original author, say sorry.

The source code is as follows:

[HTML]View Plaincopyprint?

public class Backkeytest extends activity{/** called when the Activity is first created. */@Overridepublic void OnCreate (B Undle savedinstancestate) {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;}};}

Summary:

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 of all, we have to explicitly click on three keys when the system did something, if not to listen to processing, click the Home button, the system by default only executes the application's currently displayed activity of the OnStop () method after jumping 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.

Go How Android listens for a return key, pops up an exit dialog box

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.