Android development: exit the program (dialog box, two return keys Exit)

Source: Internet
Author: User

PostCode:

 Public   Void  Exitclick (view) {alertdialog. Builder AA = New Alertdialog. Builder ( This  ); AA. settitle ( "Message prompt" ); AA. setmessage ( "Are you sure you want to exit? " ); AA. seticon (R. drawable. Icon); AA. setpositivebutton ( "OK ", New  Dialoginterface. onclicklistener () {@ override Public   Void Onclick (dialoginterface dialog, Int  Which ){  //  Todo auto-generated method stub Mainactivity. This  . Finish () ;}}); AA. setnegativebutton ( "Cancel ", Null  ); AA. Create (); AA. Show ();  //  This. Finish (); }

It feels more complicated than C...

In addition, press the return key to exit and paste the Code:

 Private   Void  Exitdialog () {alertdialog. Builder AA = New Alertdialog. Builder ( This  ); AA. settitle ( "Message prompt" ); AA. setmessage ( "Are you sure you want to exit? " ); AA. seticon (R. drawable. Icon); AA. setpositivebutton ( "OK ", New  Dialoginterface. onclicklistener () {@ override  Public  Void Onclick (dialoginterface dialog, Int  Which ){  //  Todo auto-generated method stub Mainactivity. This  . Finish () ;}}); AA. setnegativebutton ( "Cancel ", Null  ); AA. Create (); AA. Show () ;}@ override  Public   Boolean Onkeydown ( Int Keycode, keyevent event ){  If (Keycode = Keyevent. keycode_back) {exitdialog ();}  Return   True  ;} 

It is also popular to exit with two consecutive responses. Code:

     Private   Static   Boolean Isexit = False  ;  Private   Static   Boolean Hastask = False  ; Timer texit = New  Timer (); timertask task = New  Timertask () {@ override  Public   Void  Run (){  //  Todo auto-generated method stub Isexit = False  ; Hastask =True  ;}}; @ Override  Public   Boolean Onkeydown ( Int  Keycode, keyevent event ){  If (Keycode = Keyevent. keycode_back ){  //  Exitdialog ();              If (Isexit = False  ) {Isexit = True ; Toast. maketext (  This , "Exit once again Program " , Toast. length_short). Show ();  If (! Hastask) {texit. Schedule (task, 2000 );}}  Else  {Finish (); system. Exit ( 0 );}}  // Return true;          Return   False  ;} 

 

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.