• Android自定义AlertDialog


    做项目系统退出时,直接调用系统的AlertDialog感觉太难看了,于是自己定义了一番,直接上代码。

    1、自定义类MyDialog继承Dialog

      1 public class MyDialog extends Dialog {
      2  
      3     public MyDialog(Context context, int theme) {
      4         super(context, theme);
      5     }
      6  
      7     public MyDialog(Context context) {
      8         super(context);
      9     }
     10  
     11     public static class Builder {
     12  
     13         private Context context;
     14         private String title;
     15         private String message;
     16         private String positiveButtonText;
     17         private String negativeButtonText;
     18         private View contentView;
     19  
     20         private DialogInterface.OnClickListener 
     21                         positiveButtonClickListener,
     22                         negativeButtonClickListener;
     23  
     24         public Builder(Context context) {
     25             this.context = context;
     26         }
     27  
     28 
     29         public Builder setMessage(String message) {
     30             this.message = message;
     31             return this;
     32         }
     33  
     34 
     35         public Builder setMessage(int message) {
     36             this.message = (String) context.getText(message);
     37             return this;
     38         }
     39  
     40 
     41         public Builder setTitle(int title) {
     42             this.title = (String) context.getText(title);
     43             return this;
     44         }
     45  
     46 
     47         public Builder setTitle(String title) {
     48             this.title = title;
     49             return this;
     50         }
     51  
     52         public Builder setContentView(View v) {
     53             this.contentView = v;
     54             return this;
     55         }
     56  
     57 
     58         public Builder setPositiveButton(int positiveButtonText,
     59                 DialogInterface.OnClickListener listener) {
     60             this.positiveButtonText = (String) context
     61                     .getText(positiveButtonText);
     62             this.positiveButtonClickListener = listener;
     63             return this;
     64         }
     65 
     66         public Builder setPositiveButton(String positiveButtonText,
     67                 DialogInterface.OnClickListener listener) {
     68             this.positiveButtonText = positiveButtonText;
     69             this.positiveButtonClickListener = listener;
     70             return this;
     71         }
     72  
     73 
     74         public Builder setNegativeButton(int negativeButtonText,
     75                 DialogInterface.OnClickListener listener) {
     76             this.negativeButtonText = (String) context
     77                     .getText(negativeButtonText);
     78             this.negativeButtonClickListener = listener;
     79             return this;
     80         }
     81  
     82     
     83         public Builder setNegativeButton(String negativeButtonText,
     84                 DialogInterface.OnClickListener listener) {
     85             this.negativeButtonText = negativeButtonText;
     86             this.negativeButtonClickListener = listener;
     87             return this;
     88         }
     89  
     90  
     91         public MyDialog create() {
     92             LayoutInflater inflater = (LayoutInflater) context
     93                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     94             // instantiate the dialog with the custom Theme
     95             final MyDialog dialog = new MyDialog(context, 
     96                     R.style.MyDialog);
     97             View layout = inflater.inflate(R.layout.customdialog, null);
     98             dialog.addContentView(layout, new LayoutParams(
     99                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    100             ((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
    101             if (positiveButtonText != null) {
    102                 ((Button) layout.findViewById(R.id.dialog_positive))
    103                         .setText(positiveButtonText);
    104                 if (positiveButtonClickListener != null) {
    105                     ((Button) layout.findViewById(R.id.dialog_positive))
    106                             .setOnClickListener(new View.OnClickListener() {
    107                                 public void onClick(View v) {
    108                                     positiveButtonClickListener.onClick(
    109                                             dialog, 
    110                                             DialogInterface.BUTTON_POSITIVE);
    111                                 }
    112                             });
    113                 }
    114             } else {
    115                 layout.findViewById(R.id.dialog_positive).setVisibility(
    116                         View.GONE);
    117             }
    118             if (negativeButtonText != null) {
    119                 ((Button) layout.findViewById(R.id.dialog_negative))
    120                         .setText(negativeButtonText);
    121                 if (negativeButtonClickListener != null) {
    122                     ((Button) layout.findViewById(R.id.dialog_negative))
    123                             .setOnClickListener(new View.OnClickListener() {
    124                                 public void onClick(View v) {
    125                                     negativeButtonClickListener.onClick(
    126                                             dialog, 
    127                                             DialogInterface.BUTTON_NEGATIVE);
    128                                 }
    129                             });
    130                 }
    131             } else {
    132                 layout.findViewById(R.id.dialog_negative).setVisibility(
    133                         View.GONE);
    134             }
    135             if (message != null) {
    136 //                ((TextView) layout.findViewById(
    137 //                        R.id.message)).setText(message);
    138             } 
    139             else if (contentView != null) {
    140                 ((LinearLayout) layout.findViewById(R.id.content))
    141                         .removeAllViews();
    142                 ((LinearLayout) layout.findViewById(R.id.content))
    143                         .addView(contentView, 
    144                                 new LayoutParams(
    145                                         LayoutParams.WRAP_CONTENT, 
    146                                         LayoutParams.WRAP_CONTENT));
    147             }
    148             dialog.setContentView(layout);
    149             return dialog;
    150         }
    151         
    152  
    153     }

    2、定义显示的customdialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="#CFCEC8"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/dialog_title"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:gravity="center_horizontal"                android:textColor="#010101"
                    android:textSize="20dp" />
    
                <View
                    android:layout_width="fill_parent"
                    android:layout_height="1px"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/title_bar" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:gravity="center_horizontal" >
    
                <Button
                    android:id="@+id/dialog_positive"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="8dp"
                    android:background="#00000000"                android:textSize="20dp" />
    
                <View
                    android:layout_width="1px"
                    android:layout_height="fill_parent"
                    android:layout_marginLeft="20dp"
                    android:background="@drawable/title_bar" />
    
                <Button
                    android:id="@+id/dialog_negative"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="8dp"
                    android:background="#00000000"                android:textSize="20dp" />
            </LinearLayout>
        </LinearLayout>
    
    </LinearLayout>

    3、在styles里加入显示风格

    <style name="MyDialog" parent="@android:Theme.Dialog">
            <item name="android:windowFrame">@null</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@null</item>
        </style>

    使用如下

     1             Dialog dialog;
     2             MyDialog.Builder builder = new MyDialog.Builder(
     3                     JuCanBaoActivity.this);
     4             builder.setTitle("确定退出聚餐宝?");
     5             builder.setNegativeButton("取消",
     6                     new DialogInterface.OnClickListener() {
     7                         public void onClick(DialogInterface dialog, int which) {
     8                             dialog.cancel();
     9                         }
    10                     });
    11             builder.setPositiveButton("确定",
    12                     new DialogInterface.OnClickListener() {
    13                         public void onClick(DialogInterface dialog, int which) {
    14                             dialog.cancel();
    15                             JuCanBaoActivity.this.finish();
    16                             android.os.Process.killProcess(android.os.Process
    17                                     .myPid());
    18                             System.exit(0);
    19                         }
    20                     });
    21             dialog = builder.create();
    22             dialog.show();
  • 相关阅读:
    ubuntu下php-fpm多实例运行配置
    ubuntu下nginx+PHP-FPM安装配置
    php中include_path配置
    laravel中的队列
    laravel权限控制Gate
    Configuring an NVIDIA Jetson TX2
    NVIDIA Jetson TX2 の設定
    3-Jetson Nano Developer KitでAWS IoT GreengrassのML Inferenceを試す(GPU編)
    2-Jetson Nano Developer KitでAWS IoT GreengrassのML Inferenceを試す
    1-Jetson Nano Developer KitでAWS IoT Greengrassを動かしてみる
  • 原文地址:https://www.cnblogs.com/snowspace/p/3293251.html
Copyright © 2020-2023  润新知