Android uses custom AlertDialog (exit prompt box)

Source: Internet
Author: User
Sometimes we need to use a prompt box (AlertDialog) that matches our style in a game or application)


The following is my summary in developing a small game. I hope it will be useful to you.

First:

The following figure shows the background image or button image used.

After searching for materials and referring to the example below, it is very easy to achieve this effect. It is to set the contentView of alertDialog. The following code is written in Activity, and the code is as follows:

  1. Public boolean onKeyDown (int keyCode, KeyEvent event ){
  2. // If the return key is used, it is directly returned to the desktop.
  3. If (keyCode = KeyEvent. KEYCODE_BACK | keyCode = KeyEvent. KEYCODE_HOME ){
  4. ShowExitGameAlert ();
  5. }
  6. Return super. onKeyDown (keyCode, event );
  7. }
  8. Private void showExitGameAlert (){
  9. Final AlertDialog dlg = new AlertDialog. Builder (this). create ();
  10. Dlg. show ();
  11. Window window = dlg. getWindow ();
  12. // *** This effect is implemented here.
  13. // Set the content page of the window. The view content is defined in the shrew_exit_dialog.xml file.
  14. Window. setContentView (R. layout. shrew_exit_dialog );
  15. // Add an event for the confirm button and exit the application
  16. ImageButton OK = (ImageButton) window. findViewById (R. id. btn_ OK );
  17. OK. setOnClickListener (new View. OnClickListener (){
  18. Public void onClick (View v ){
  19. ExitApp (); // exit the application...
  20. }
  21. });
  22. // Close the alert dialog framework
  23. ImageButton cancel = (ImageButton) window. findViewById (R. id. btn_cancel );
  24. Cancel. setOnClickListener (new View. OnClickListener (){
  25. Public void onClick (View v ){
  26. Dlg. cancel ();
  27. }
  28. });
  29. }

Copy code

The layout file defines the background and buttons in the dialog box. Click the event to add it to the Activity.
The file name is:Shrew_exit_dialog.xml

  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <RelativeLayout
  3. Xmlns: android = "http://schemas.android.com/apk/res/android"
  4. Android: layout_height = "wrap_content"
  5. Android: layout_width = "wrap_content">
  6. <! -- Background image for exiting the game -->
  7. <ImageView android: id = "@ + id/exitGameBackground"
  8. Android: layout_centerInParent = "true"
  9. Android: layout_height = "wrap_content"
  10. Android: layout_width = "wrap_content"
  11. Android: src = "@ drawable/bg_exit_game"/>
  12. <! -- Confirm button -->
  13. <ImageButton android: layout_alignBottom = "@ + id/exitGameBackground"
  14. Android: layout_alignLeft = "@ + id/exitGameBackground"
  15. Android: layout_marginBottom = "30dp"
  16. Android: layout_marginLeft = "35dp"
  17. Android: id = "@ + id/btn_ OK"
  18. Android: layout_height = "wrap_content"
  19. Android: layout_width = "wrap_content"
  20. Android: background = "@ drawable/btn_ OK"/>
  21. <! -- Cancel button -->
  22. <ImageButton android: layout_alignBottom = "@ + id/exitGameBackground"
  23. Android: layout_alignRight = "@ + id/exitGameBackground"
  24. Android: layout_marginBottom = "30dp"
  25. Android: layout_marginRight = "35dp"
  26. Android: id = "@ + id/btn_cancel"
  27. Android: layout_height = "wrap_content"
  28. Android: layout_width = "wrap_content"
  29. Android: background = "@ drawable/btn_cancel"/>
  30. </RelativeLayout>

Copy code

After completing the preceding steps, you can customize AlertDialog and use the same idea to implement other more complex effects.

Related Article

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.