Android Custom Alertdialog (Exit Prompt box)

Source: Internet
Author: User

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)

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.