Android Monitor Return key

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:18px">Android in development, monitoring keys returned to post events are often used in the following example to illustrate what the Android Return key event listener is.</span></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="498931" snippet_file_name="blog_20141027_1_1697833" name="code" class="java">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 dialog Message Isexit.setmessage ("ok to Exit");//add Select button and register for monitoring 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;}};}</pre><br>Android phones often use three keys, home key, back key and menu Key.<br>In the application, we often do a certain amount of processing, convenient for users to Use.<p><p></p></p><p><p><br>First of all, we need to understand what the system did when we clicked three keys,<br>Assuming no listening processing is performed,<br>When you click the home key, the system defaults to simply running the Application's currently displayed Activity's OnStop () method and jumping out of the interface.<br>When you click the back key, the system runs by default the finish () method of the current program activity is used to jump the Interface.</p></p><p><p><br>and click the menu key. By default, no matter what the Method.<br><br><br>Here is just a simple application demo, we can according to their own needs to design a more complete Exit dialog box.<br><br><br>Be able to listen to other treatments and return to the Event. There's a lot More.</p></p><p><p><br></p></p> <p><p>Android Monitor Return key</p></p></span>

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.