1 Public classMainactivityextendsActivity {2 3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8 }9 Ten LongStartTime; One A @Override - Public voidonbackpressed () { - if(System.currenttimemillis ()-StartTime < 2000) { the Super. onbackpressed (); -}Else { -StartTime =System.currenttimemillis (); - +Toast toast =NewToast ( This); - +LinearLayout layout =NewLinearLayout ( This); A layout.setorientation (linearlayout.vertical); at Layout.setbackgroundcolor (color.yellow); - -TextView TV1 =NewTextView ( This); -Tv1.settext ("Hint"); - tv1.setgravity (gravity.center); - inTextView TV2 =NewTextView ( This); -Tv2.settext ("Press the Back button again to exit"); to tv2.setgravity (gravity.center); + - Layout.addview (TV1); the Layout.addview (TV2); * $ Toast.setview (layout);Panax NotoginsengToast.setgravity (gravity.center_vertical | Gravity.fill_horizontal, 0,-200); - toast.setduration (toast.length_short); the toast.show (); + A } the + } - $}mainactivity
Android Toast Tip Press two times back key to exit