"Go" Android implementation "press again to exit the program"

Source: Internet
Author: User

from:http://blog.csdn.net/ldj299/article/details/7574365

Personally feel that when the user presses the Back button, the prompt "Press again to exit" prevents the error from being manipulated more humanely than a dialog box is displayed. Words not much to say on the code:

The first type: rewrite onkeydown to listen:

    LongWaitTime = 2000; LongTouchtime = 0; @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        if(event.getaction () = = Keyevent.action_down && Keyevent.keycode_back = =keycode) {            LongCurrentTime =System.currenttimemillis (); if((Currenttime-touchtime) >=waitTime) {Toast.maketext ( This, "Press again to exit", Toast.length_short). Show (); Touchtime=currenttime; }Else{finish (); }            return true; }        return Super. OnKeyDown (KeyCode, event); }

The second is to override the Onbackpressed method to directly listen for the return key:

@Override      Public void onbackpressed () {//        super.onbackpressed ();        long currenttime = system.currenttimemillis ();         if ((currenttime-touchtime) >= waitTime) {            Toast.maketext (this, "Press Exit Again", Toast.length_short). Show ();             = currenttime;        } Else {            finish ();        }    }

"Go" Android implementation "press again to exit the program"

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.