@Override Public voidonbackpressed () {NewAlertdialog.builder ( This). Settitle ("Confirm exit?"). "). SetIcon (Android. R.drawable.ic_dialog_info). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //Click "Confirm" after the actionMainfragmentactivity. This. Finish (); }}). Setnegativebutton ("Back",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //Click "Back" after the action, not set here No action}}). Show (); //super.onbackpressed ();}
Below is a picture of the background image or button used
After looking up the data and referring to the example before we know that to achieve this effect is very simple. is in setting Alertdialog Contentview.
The following code is written in the activity, the code is as follows:
Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { //if it is a return key, return directly to the desktop if(keycode = = Keyevent.keycode_back | | keycode = =keyevent.keycode_home) {Showexitgamealert ();} return Super. OnKeyDown (KeyCode, event);}Private voidShowexitgamealert () {FinalAlertdialog dlg =NewAlertdialog.builder ( This). Create (); Dlg.show (); Window Window=Dlg.getwindow (); //* * * is mainly here to achieve this effect. //set the content page of the window, define the view content in the Shrew_exit_dialog.xml fileWindow.setcontentview (R.layout.shrew_exit_dialog); //add an event to the Confirm button to perform an exit app operationImageButton OK =(ImageButton) Window.findviewbyid (R.ID.BTN_OK); Ok.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {Exitapp ();//quit App ... } }); //Close Alert Dialog FrameImageButton Cancel =(ImageButton) Window.findviewbyid (r.id.btn_cancel); Cancel.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {dlg.cancel (); } });}
The following is the layout file, which defines the background and buttons in the dialog box. Click events to add to the activity.
File name is: Shrew_exit_dialog.xml
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"> <!--exit the game's background image - <ImageViewAndroid:id= "@+id/exitgamebackground"android:layout_centerinparent= "true"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"android:src= "@drawable/bg_exit_game" /> <!--Confirm Button - <ImageButtonAndroid:layout_alignbottom= "@+id/exitgamebackground"Android:layout_alignleft= "@+id/exitgamebackground"Android:layout_marginbottom= "30DP"Android:layout_marginleft= "35DP"Android:id= "@+id/btn_ok"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:background= "@drawable/btn_ok" /> <!--Cancel Button - <ImageButtonAndroid:layout_alignbottom= "@+id/exitgamebackground"Android:layout_alignright= "@+id/exitgamebackground"Android:layout_marginbottom= "30DP"Android:layout_marginright= "35DP"Android:id= "@+id/btn_cancel"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:background= "@drawable/btn_cancel" /></Relativelayout>
This way, you can achieve the effect of customizing the Alertdialog by following the steps above. Other more complex effects can be achieved with the same ideas.
Android Implementation "Exit confirmation" dialog box