Sometimes we need to use some type of cue box (Alertdialog) in our game or app, and here's what I've summed up in developing a little game. I hope it will be useful to you.
First up Effect chart:
Here is a picture of the background image or button used
After looking at the data and reference to a few examples, we know that to achieve this effect is very simple. is to set the Alertdialog Contentview.
The following code is written under the activity, as follows:
public boolean onKeyDown (int keycode, keyevent event) {//If return key, return directly to desktop if (keycode = = Keyevent.keycode_back | | keycode
= = Keyevent.keycode_home) {Showexitgamealert ();} return Super.onkeydown (KeyCode, event);
private void Showexitgamealert () {final Alertdialog dlg = new Alertdialog.builder (this). Create (); Dlg.show ();
window window = Dlg.getwindow ();
The main thing is to achieve this effect here.
Sets the content page of the window, defines the view content Window.setcontentview (R.layout.shrew_exit_dialog) in the Shrew_exit_dialog.xml file;
To add an event for the confirmation button, perform the exit application operation ImageButton OK = (ImageButton) Window.findviewbyid (R.ID.BTN_OK);
Ok.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {Exitapp ();//Exit Application ...}});
Close the alert dialog frame ImageButton Cancel = (ImageButton) Window.findviewbyid (r.id.btn_cancel);
Cancel.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {dlg.cancel ();}});
The following is a layout file that defines the background and buttons in the dialog box. The Click event is added in the activity. File name: 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 map--> <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"/> <!--confirmation 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 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> In this way after a few steps, you can realize the effect of custom alertdialog. The same idea can be used to achieve other more complex effects.
Alertdialog Implementation Confirmation Exit button Instance code:
Package com.example.alertdialog;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.Toast; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate
(savedinstancestate);
Setcontentview (R.layout.activity_main);
//Name if it is onbackpressed, that is to press the phone return key effect, the parameters are empty. public void onBackPressed1 (View v) {new Alertdialog.builder (this). Settitle (Confirm exit?) "). SetIcon (Android. R.drawable.ic_dialog_info). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void Oncl
Ick (dialoginterface dialog, int which) {//click "Confirm" after Operation MainActivity.this.finish (); }). Setnegativebutton ("Back", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface di Alog, int which) {//click "Back" after the operation, here does not set any operation Toast.maketext (Mainactivity.this, "You clicked the Return key", ToaSt.
Length_long). Show ();
). Show (); }
}