Android:自定义dialog

本文介绍如何在Android应用中创建自定义样式的对话框。通过定义对话框的布局、主题和样式,可以实现更加美观和统一的用户体验。此外,还提供了一个自定义的Dialog和Builder类,以方便地构建这些自定义对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 

原文地址:http://www.open-china.net/blog/42853.html

Android给我们提供了 AlertDialog类,使我们可以很容易地通过它的内部类Builder构建弹出式对话框。但是有时候为了给用户更好的体验,可能需要更改Dialog的外观和结构。解决的办法是自定义AlertDialog和AlertDialog.Builder类。 
Android提供的默认Dialog如下图所示: 
 

1、定义对话框的外观 

我们想实现的自定义对话框如下图所示: 
 

我们要实现的Dialog支持: 

  • 通过外部String或Resource命名Title
  • 通过外部String、layout、Resource定义对话框内容
  • 设置了positive和negative按钮及监听器


2、定义Layout、Theme和Style 

对话框通过自定义布局(layout)渲染其内容,布局文件中定义了用于显示标题的TextView、显示内容的TextView,以及两个按钮。 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:minWidth="280dip"  
  6.     android:layout_height="wrap_content">  
  7.    
  8.     <LinearLayout   
  9.         android:orientation="vertical"  
  10.         android:background="@drawable/header"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content">  
  13.    
  14.         <TextView  
  15.             style="@style/DialogText.Title"  
  16.             android:id="@+id/title"  
  17.             android:paddingRight="8dip"  
  18.             android:paddingLeft="8dip"  
  19.             android:background="@drawable/title"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"/>  
  22.    
  23.     </LinearLayout>  
  24.    
  25.     <LinearLayout   
  26.         android:id="@+id/content"  
  27.         android:orientation="vertical"  
  28.         android:background="@drawable/center"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content">  
  31.    
  32.         <TextView  
  33.             style="@style/DialogText"  
  34.             android:id="@+id/message"  
  35.             android:padding="5dip"  
  36.             android:layout_width="fill_parent"  
  37.             android:layout_height="wrap_content"/>  
  38.    
  39.     </LinearLayout>  
  40.    
  41.     <LinearLayout   
  42.         android:orientation="horizontal"  
  43.         android:background="@drawable/footer"  
  44.         android:layout_width="fill_parent"  
  45.         android:layout_height="wrap_content">  
  46.    
  47.         <Button   
  48.             android:id="@+id/positiveButton"  
  49.             android:layout_marginTop="3dip"  
  50.             android:layout_width="0dip"  
  51.             android:layout_weight="1"  
  52.             android:layout_height="wrap_content"  
  53.             android:singleLine="true"/>  
  54.    
  55.         <Button   
  56.             android:id="@+idegativeButton"  
  57.             android:layout_marginTop="3dip"  
  58.             android:layout_width="0dip"  
  59.             android:layout_weight="1"  
  60.             android:layout_height="wrap_content"  
  61.             android:singleLine="true"/>  
  62.    
  63.     </LinearLayout>  
  64.    
  65. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:minWidth="280dip"
    android:layout_height="wrap_content">
 
    <LinearLayout 
        android:orientation="vertical"
        android:background="@drawable/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
 
        <TextView
            style="@style/DialogText.Title"
            android:id="@+id/title"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:background="@drawable/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
 
    </LinearLayout>
 
    <LinearLayout 
        android:id="@+id/content"
        android:orientation="vertical"
        android:background="@drawable/center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
 
        <TextView
            style="@style/DialogText"
            android:id="@+id/message"
            android:padding="5dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
 
    </LinearLayout>
 
    <LinearLayout 
        android:orientation="horizontal"
        android:background="@drawable/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
 
        <Button 
            android:id="@+id/positiveButton"
            android:layout_marginTop="3dip"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:singleLine="true"/>
 
        <Button 
            android:id="@+idegativeButton"
            android:layout_marginTop="3dip"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:singleLine="true"/>
 
    </LinearLayout>
 
</LinearLayout>

根部的LinearLayou宽度被设置为fill_parent,并且最小宽度为280dp,从而使对话框的宽度始终是设备屏幕宽度的87.5%。 

自定义主题应该声明对话框为floating ,并且使用了自定义背景和自定义标题视图。
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    
  4.     <style name="Dialog" parent="android:style/Theme.Dialog">  
  5.         <item name="android:windowBackground">@null</item>  
  6.         <item name="android:windowNoTitle">true</item>  
  7.         <item name="android:windowIsFloating">true</item>  
  8.     </style>  
  9.    
  10. </resources>  
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <style name="Dialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>
 
</resources>

然后我们需要定义标题和内容的外观 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    
  4.     <style name="DialogText">  
  5.         <item name="android:textColor">#FF000000</item>  
  6.         <item name="android:textSize">12sp</item>  
  7.     </style>  
  8.    
  9.     <style name="DialogText.Title">  
  10.         <item name="android:textSize">16sp</item>  
  11.         <item name="android:textStyle">bold</item>  
  12.     </style>  
  13.    
  14. </resources>  
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <style name="DialogText">
        <item name="android:textColor">#FF000000</item>
        <item name="android:textSize">12sp</item>
    </style>
 
    <style name="DialogText.Title">
        <item name="android:textSize">16sp</item>
        <item name="android:textStyle">bold</item>
    </style>
 
</resources>


3、定义Dialog和Builder类 
我们自定义的Builder类最好和AlterDialog.Bulider类有相同的方法,以便使用方便。

  1. package net.androgames.blog.sample.customdialog.dialog;  
  2.    
  3. import net.androgames.blog.sample.customdialog.R;  
  4. import android.app.Dialog;  
  5. import android.content.Context;  
  6. import android.content.DialogInterface;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup.LayoutParams;  
  10. import android.widget.Button;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.TextView;  
  13.    
  14. /** 
  15.  *  
  16.  * Create custom Dialog windows for your application 
  17.  * Custom dialogs rely on custom layouts wich allow you to  
  18.  * create and use your own look & feel. 
  19.  *  
  20.  * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html 
  21.  *  
  22.  * @author antoine vianey 
  23.  * 
  24.  */  
  25. public class CustomDialog extends Dialog {  
  26.    
  27.     public CustomDialog(Context context, int theme) {  
  28.         super(context, theme);  
  29.     }  
  30.    
  31.     public CustomDialog(Context context) {  
  32.         super(context);  
  33.     }  
  34.    
  35.     /** 
  36.      * Helper class for creating a custom dialog 
  37.      */  
  38.     public static class Builder {  
  39.    
  40.         private Context context;  
  41.         private String title;  
  42.         private String message;  
  43.         private String positiveButtonText;  
  44.         private String negativeButtonText;  
  45.         private View contentView;  
  46.    
  47.         private DialogInterface.OnClickListener   
  48.                         positiveButtonClickListener,  
  49.                         negativeButtonClickListener;  
  50.    
  51.         public Builder(Context context) {  
  52.             this.context = context;  
  53.         }  
  54.    
  55.         /** 
  56.          * Set the Dialog message from String 
  57.          * @param title 
  58.          * @return 
  59.          */  
  60.         public Builder setMessage(String message) {  
  61.             this.message = message;  
  62.             return this;  
  63.         }  
  64.    
  65.         /** 
  66.          * Set the Dialog message from resource 
  67.          * @param title 
  68.          * @return 
  69.          */  
  70.         public Builder setMessage(int message) {  
  71.             this.message = (String) context.getText(message);  
  72.             return this;  
  73.         }  
  74.    
  75.         /** 
  76.          * Set the Dialog title from resource 
  77.          * @param title 
  78.          * @return 
  79.          */  
  80.         public Builder setTitle(int title) {  
  81.             this.title = (String) context.getText(title);  
  82.             return this;  
  83.         }  
  84.    
  85.         /** 
  86.          * Set the Dialog title from String 
  87.          * @param title 
  88.          * @return 
  89.          */  
  90.         public Builder setTitle(String title) {  
  91.             this.title = title;  
  92.             return this;  
  93.         }  
  94.    
  95.         /** 
  96.          * Set a custom content view for the Dialog. 
  97.          * If a message is set, the contentView is not 
  98.          * added to the Dialog... 
  99.          * @param v 
  100.          * @return 
  101.          */  
  102.         public Builder setContentView(View v) {  
  103.             this.contentView = v;  
  104.             return this;  
  105.         }  
  106.    
  107.         /** 
  108.          * Set the positive button resource and it"s listener 
  109.          * @param positiveButtonText 
  110.          * @param listener 
  111.          * @return 
  112.          */  
  113.         public Builder setPositiveButton(int positiveButtonText,  
  114.                 DialogInterface.OnClickListener listener) {  
  115.             this.positiveButtonText = (String) context  
  116.                     .getText(positiveButtonText);  
  117.             this.positiveButtonClickListener = listener;  
  118.             return this;  
  119.         }  
  120.    
  121.         /** 
  122.          * Set the positive button text and it"s listener 
  123.          * @param positiveButtonText 
  124.          * @param listener 
  125.          * @return 
  126.          */  
  127.         public Builder setPositiveButton(String positiveButtonText,  
  128.                 DialogInterface.OnClickListener listener) {  
  129.             this.positiveButtonText = positiveButtonText;  
  130.             this.positiveButtonClickListener = listener;  
  131.             return this;  
  132.         }  
  133.    
  134.         /** 
  135.          * Set the negative button resource and it"s listener 
  136.          * @param negativeButtonText 
  137.          * @param listener 
  138.          * @return 
  139.          */  
  140.         public Builder setNegativeButton(int negativeButtonText,  
  141.                 DialogInterface.OnClickListener listener) {  
  142.             this.negativeButtonText = (String) context  
  143.                     .getText(negativeButtonText);  
  144.             this.negativeButtonClickListener = listener;  
  145.             return this;  
  146.         }  
  147.    
  148.         /** 
  149.          * Set the negative button text and it"s listener 
  150.          * @param negativeButtonText 
  151.          * @param listener 
  152.          * @return 
  153.          */  
  154.         public Builder setNegativeButton(String negativeButtonText,  
  155.                 DialogInterface.OnClickListener listener) {  
  156.             this.negativeButtonText = negativeButtonText;  
  157.             this.negativeButtonClickListener = listener;  
  158.             return this;  
  159.         }  
  160.    
  161.         /** 
  162.          * Create the custom dialog 
  163.          */  
  164.         public CustomDialog create() {  
  165.             LayoutInflater inflater = (LayoutInflater) context  
  166.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  167.             // instantiate the dialog with the custom Theme  
  168.             final CustomDialog dialog = new CustomDialog(context,   
  169.                     R.style.Dialog);  
  170.             View layout = inflater.inflate(R.layout.dialog, null);  
  171.             dialog.addContentView(layout, new LayoutParams(  
  172.                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  173.             // set the dialog title  
  174.             ((TextView) layout.findViewById(R.id.title)).setText(title);  
  175.             // set the confirm button  
  176.             if (positiveButtonText != null) {  
  177.                 ((Button) layout.findViewById(R.id.positiveButton))  
  178.                         .setText(positiveButtonText);  
  179.                 if (positiveButtonClickListener != null) {  
  180.                     ((Button) layout.findViewById(R.id.positiveButton))  
  181.                             .setOnClickListener(new View.OnClickListener() {  
  182.                                 public void onClick(View v) {  
  183.                                     positiveButtonClickListener.onClick(  
  184.                                             dialog,   
  185.                                             DialogInterface.BUTTON_POSITIVE);  
  186.                                 }  
  187.                             });  
  188.                 }  
  189.             } else {  
  190.                 // if no confirm button just set the visibility to GONE  
  191.                 layout.findViewById(R.id.positiveButton).setVisibility(  
  192.                         View.GONE);  
  193.             }  
  194.             // set the cancel button  
  195.             if (negativeButtonText != null) {  
  196.                 ((Button) layout.findViewById(R.id.negativeButton))  
  197.                         .setText(negativeButtonText);  
  198.                 if (negativeButtonClickListener != null) {  
  199.                     ((Button) layout.findViewById(R.id.negativeButton))  
  200.                             .setOnClickListener(new View.OnClickListener() {  
  201.                                 public void onClick(View v) {  
  202.                                     positiveButtonClickListener.onClick(  
  203.                                             dialog,   
  204.                                             DialogInterface.BUTTON_NEGATIVE);  
  205.                                 }  
  206.                             });  
  207.                 }  
  208.             } else {  
  209.                 // if no confirm button just set the visibility to GONE  
  210.                 layout.findViewById(R.id.negativeButton).setVisibility(  
  211.                         View.GONE);  
  212.             }  
  213.             // set the content message  
  214.             if (message != null) {  
  215.                 ((TextView) layout.findViewById(  
  216.                         R.id.message)).setText(message);  
  217.             } else if (contentView != null) {  
  218.                 // if no message set  
  219.                 // add the contentView to the dialog body  
  220.                 ((LinearLayout) layout.findViewById(R.id.content))  
  221.                         .removeAllViews();  
  222.                 ((LinearLayout) layout.findViewById(R.id.content))  
  223.                         .addView(contentView,   
  224.                                 new LayoutParams(  
  225.                                         LayoutParams.WRAP_CONTENT,   
  226.                                         LayoutParams.WRAP_CONTENT));  
  227.             }  
  228.             dialog.setContentView(layout);  
  229.             return dialog;  
  230.         }  
  231.    
  232.     }  
  233.    
  234. }  
package net.androgames.blog.sample.customdialog.dialog;
 
import net.androgames.blog.sample.customdialog.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
 
/**
 * 
 * Create custom Dialog windows for your application
 * Custom dialogs rely on custom layouts wich allow you to 
 * create and use your own look & feel.
 * 
 * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 * 
 * @author antoine vianey
 *
 */
public class CustomDialog extends Dialog {
 
    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }
 
    public CustomDialog(Context context) {
        super(context);
    }
 
    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {
 
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
 
        private DialogInterface.OnClickListener 
                        positiveButtonClickListener,
                        negativeButtonClickListener;
 
        public Builder(Context context) {
            this.context = context;
        }
 
        /**
         * Set the Dialog message from String
         * @param title
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }
 
        /**
         * Set the Dialog message from resource
         * @param title
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }
 
        /**
         * Set the Dialog title from resource
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }
 
        /**
         * Set the Dialog title from String
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
 
        /**
         * Set a custom content view for the Dialog.
         * If a message is set, the contentView is not
         * added to the Dialog...
         * @param v
         * @return
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }
 
        /**
         * Set the positive button resource and it"s listener
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        /**
         * Set the positive button text and it"s listener
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String positiveButtonText,
                DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        /**
         * Set the negative button resource and it"s listener
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(int negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        /**
         * Set the negative button text and it"s listener
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(String negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        /**
         * Create the custom dialog
         */
        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context, 
            		R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(
                                    		dialog, 
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(
                                    		dialog, 
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(
                		R.id.message)).setText(message);
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, 
                                new LayoutParams(
                                        LayoutParams.WRAP_CONTENT, 
                                        LayoutParams.WRAP_CONTENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }
 
    }
 
}


4、使用自定义的Bulider类 

  1. /** 
  2.  * Build the desired Dialog 
  3.  * CUSTOM or DEFAULT 
  4.  */  
  5. @Override  
  6. public Dialog onCreateDialog(int dialogId) {  
  7.     Dialog dialog = null;  
  8.     switch (dialogId) {  
  9.         case CUSTOM_DIALOG :  
  10.             CustomDialog.Builder customBuilder = new  
  11.                 CustomDialog.Builder(CustomDialogActivity.this);  
  12.             customBuilder.setTitle("Custom title")  
  13.                 .setMessage("Custom body")  
  14.                 .setNegativeButton("Cancel",   
  15.                         new DialogInterface.OnClickListener() {  
  16.                     public void onClick(DialogInterface dialog, int which) {  
  17.                         CustomDialogActivity.this  
  18.                         .dismissDialog(CUSTOM_DIALOG);  
  19.                     }  
  20.                 })  
  21.                 .setPositiveButton("Confirm",   
  22.                         new DialogInterface.OnClickListener() {  
  23.                     public void onClick(DialogInterface dialog, int which) {  
  24.                         dialog.dismiss();  
  25.                     }  
  26.                 });  
  27.             dialog = customBuilder.create();  
  28.             break;  
  29.         case DEFAULT_DIALOG :  
  30.             AlertDialog.Builder alertBuilder = new  
  31.                 AlertDialog.Builder(CustomDialogActivity.this);  
  32.             alertBuilder.setTitle("Default title")  
  33.                 .setMessage("Default body")  
  34.                 .setNegativeButton("Cancel",   
  35.                         new DialogInterface.OnClickListener() {  
  36.                     public void onClick(DialogInterface dialog, int which) {  
  37.                         dialog.dismiss();  
  38.                     }  
  39.                 })  
  40.                 .setPositiveButton("Confirm",   
  41.                         new DialogInterface.OnClickListener() {  
  42.                     public void onClick(DialogInterface dialog, int which) {  
  43.                         CustomDialogActivity.this  
  44.                         .dismissDialog(DEFAULT_DIALOG);  
  45.                     }  
  46.                 });  
  47.             dialog = alertBuilder.create();  
  48.             break;  
  49.     }  
  50.     return dialog;  
  51. }  
/**
 * Build the desired Dialog
 * CUSTOM or DEFAULT
 */
@Override
public Dialog onCreateDialog(int dialogId) {
    Dialog dialog = null;
    switch (dialogId) {
        case CUSTOM_DIALOG :
            CustomDialog.Builder customBuilder = new
                CustomDialog.Builder(CustomDialogActivity.this);
            customBuilder.setTitle("Custom title")
                .setMessage("Custom body")
                .setNegativeButton("Cancel", 
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        CustomDialogActivity.this
                        .dismissDialog(CUSTOM_DIALOG);
                    }
                })
                .setPositiveButton("Confirm", 
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
            dialog = customBuilder.create();
            break;
        case DEFAULT_DIALOG :
            AlertDialog.Builder alertBuilder = new
                AlertDialog.Builder(CustomDialogActivity.this);
            alertBuilder.setTitle("Default title")
                .setMessage("Default body")
                .setNegativeButton("Cancel", 
                		new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("Confirm", 
                		new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        CustomDialogActivity.this
                        .dismissDialog(DEFAULT_DIALOG);
                    }
                });
            dialog = alertBuilder.create();
            break;
    }
    return dialog;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值