Sometimes we need to use some cue boxes in our game or app that match our style (Alertdialog)
Here's what I've summed up in developing a small game. I hope it will be useful to everyone.
First on:
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:
PublicBoolean OnKeyDown (intKeyCode, KeyEventEvent) { //if it is a return key, return directly to the desktop if(keycode = = Keyevent.keycode_back | | keycode = =keyevent.keycode_home) {Showexitgamealert ();} returnSuper.onkeydown (KeyCode,Event);}Private voidShowexitgamealert () {final Alertdialog 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"?><relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_height="wrap_content"Android:layout_width="wrap_content"> <!--exit game background--<imageview android: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--<imagebutton android: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-<imagebutton android: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>
Android Custom Alertdialog (Exit Prompt box)