精美舒适的对话消息提示框--第三方开源--SweetAlertDialog

SweetAlertDialog(sweet-alert-dialog)是一个套制作精美、动画效果出色生动的Android对话、消息提示框

SweetAlertDialog(sweet-alert-dialog)在github上的项目主页是:https://github.com/pedant/sweet-alert-dialog
需要注意的是,SweetAlertDialog(sweet-alert-dialog)作为库,其自身又依赖另外一个github上的开源库materialish-progress(其在github上的项目主页是:https://github.com/pnikosis/materialish-progress )。如果使用SweetAlertDialog(sweet-alert-dialog),则需要再把materialish-progress也导入到Eclipse中作为库被SweetAlertDialog(sweet-alert-dialog)引用。

下面是demo代码:

MainActivity.java:

  1 package cn.pedant.SweetAlert.sample;
  2 
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.os.CountDownTimer;
  6 import android.view.View;
  7 
  8 import cn.pedant.SweetAlert.SweetAlertDialog;
  9 
 10 public class SampleActivity extends Activity implements View.OnClickListener {
 11 
 12     private int i = -1;
 13 
 14     @Override
 15     public void onCreate(Bundle savedInstanceState) {
 16         super.onCreate(savedInstanceState);
 17         setContentView(R.layout.sample_activity);
 18         findViewById(R.id.basic_test).setOnClickListener(this);
 19         findViewById(R.id.under_text_test).setOnClickListener(this);
 20         findViewById(R.id.error_text_test).setOnClickListener(this);
 21         findViewById(R.id.success_text_test).setOnClickListener(this);
 22         findViewById(R.id.warning_confirm_test).setOnClickListener(this);
 23         findViewById(R.id.warning_cancel_test).setOnClickListener(this);
 24         findViewById(R.id.custom_img_test).setOnClickListener(this);
 25         findViewById(R.id.progress_dialog).setOnClickListener(this);
 26     }
 27 
 28     @Override
 29     public void onClick(View v) {
 30         switch (v.getId()) {
 31             case R.id.basic_test:
 32                 // default title "Here's a message!"
 33                 SweetAlertDialog sd = new SweetAlertDialog(this);
 34                 sd.setCancelable(true);
 35                 sd.setCanceledOnTouchOutside(true);
 36                 sd.show();
 37                 break;
 38             case R.id.under_text_test:
 39                 new SweetAlertDialog(this)
 40                         .setContentText("It's pretty, isn't it?")
 41                         .show();
 42                 break;
 43             case R.id.error_text_test:
 44                 new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
 45                         .setTitleText("Oops...")
 46                         .setContentText("Something went wrong!")
 47                         .show();
 48                 break;
 49             case R.id.success_text_test:
 50                 new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE)
 51                         .setTitleText("Good job!")
 52                         .setContentText("You clicked the button!")
 53                         .show();
 54                 break;
 55             case R.id.warning_confirm_test:
 56                 new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
 57                         .setTitleText("Are you sure?")
 58                         .setContentText("Won't be able to recover this file!")
 59                         .setConfirmText("Yes,delete it!")
 60                         .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
 61                         @Override
 62                         public void onClick(SweetAlertDialog sDialog) {
 63                             // reuse previous dialog instance
 64                             sDialog.setTitleText("Deleted!")
 65                                     .setContentText("Your imaginary file has been deleted!")
 66                                     .setConfirmText("OK")
 67                                     .setConfirmClickListener(null)
 68                                     .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
 69                         }
 70                         })
 71                         .show();
 72                 break;
 73             case R.id.warning_cancel_test:
 74                 new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
 75                         .setTitleText("Are you sure?")
 76                         .setContentText("Won't be able to recover this file!")
 77                         .setCancelText("No,cancel plx!")
 78                         .setConfirmText("Yes,delete it!")
 79                         .showCancelButton(true)
 80                         .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
 81                             @Override
 82                             public void onClick(SweetAlertDialog sDialog) {
 83                                 // reuse previous dialog instance, keep widget user state, reset them if you need
 84                                 sDialog.setTitleText("Cancelled!")
 85                                         .setContentText("Your imaginary file is safe :)")
 86                                         .setConfirmText("OK")
 87                                         .showCancelButton(false)
 88                                         .setCancelClickListener(null)
 89                                         .setConfirmClickListener(null)
 90                                         .changeAlertType(SweetAlertDialog.ERROR_TYPE);
 91 
 92                                 // or you can new a SweetAlertDialog to show
 93                                /* sDialog.dismiss();
 94                                 new SweetAlertDialog(SampleActivity.this, SweetAlertDialog.ERROR_TYPE)
 95                                         .setTitleText("Cancelled!")
 96                                         .setContentText("Your imaginary file is safe :)")
 97                                         .setConfirmText("OK")
 98                                         .show();*/
 99                             }
100                         })
101                         .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
102                             @Override
103                             public void onClick(SweetAlertDialog sDialog) {
104                                 sDialog.setTitleText("Deleted!")
105                                         .setContentText("Your imaginary file has been deleted!")
106                                         .setConfirmText("OK")
107                                         .showCancelButton(false)
108                                         .setCancelClickListener(null)
109                                         .setConfirmClickListener(null)
110                                         .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
111                             }
112                         })
113                         .show();
114                 break;
115             case R.id.custom_img_test:
116                 new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
117                         .setTitleText("Sweet!")
118                         .setContentText("Here's a custom image.")
119                         .setCustomImage(R.drawable.custom_img)
120                         .show();
121                 break;
122             case R.id.progress_dialog:
123                 final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
124                         .setTitleText("Loading");
125                 pDialog.show();
126                 pDialog.setCancelable(false);
127                 new CountDownTimer(800 * 7, 800) {
128                     public void onTick(long millisUntilFinished) {
129                         // you can change the progress bar color by ProgressHelper every 800 millis
130                         i++;
131                         switch (i){
132                             case 0:
133                                 pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
134                                 break;
135                             case 1:
136                                 pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50));
137                                 break;
138                             case 2:
139                                 pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
140                                 break;
141                             case 3:
142                                 pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20));
143                                 break;
144                             case 4:
145                                 pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80));
146                                 break;
147                             case 5:
148                                 pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color));
149                                 break;
150                             case 6:
151                                 pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
152                                 break;
153                         }
154                     }
155 
156                     public void onFinish() {
157                         i = -1;
158                         pDialog.setTitleText("Success!")
159                                 .setConfirmText("OK")
160                                 .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
161                     }
162                 }.start();
163                 break;
164         }
165     }
166 }

sample_activity.xml:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:background="#FFF" >
  6 
  7     <RelativeLayout
  8         android:layout_width="match_parent"
  9         android:layout_height="wrap_content"
 10         android:paddingBottom="10dp" >
 11 
 12         <ImageView
 13             android:id="@+id/logo_img"
 14             android:layout_width="180dp"
 15             android:layout_height="wrap_content"
 16             android:layout_centerHorizontal="true"
 17             android:layout_marginBottom="15dp"
 18             android:layout_marginTop="10dp"
 19             android:contentDescription="@string/app_name"
 20             android:src="@drawable/logo_big" />
 21 
 22         <TextView
 23             android:id="@+id/txt_0"
 24             android:layout_width="wrap_content"
 25             android:layout_height="wrap_content"
 26             android:layout_alignLeft="@id/logo_img"
 27             android:layout_below="@id/logo_img"
 28             android:layout_marginLeft="15dp"
 29             android:text="show material progress"
 30             android:textColor="#797979"
 31             android:textSize="14sp" />
 32 
 33         <Button
 34             android:id="@+id/progress_dialog"
 35             style="@style/dialog_blue_button"
 36             android:layout_below="@id/txt_0"
 37             android:layout_centerHorizontal="true"
 38             android:layout_margin="10dp"
 39             android:text="Try me!" />
 40 
 41         <TextView
 42             android:id="@+id/txt_1"
 43             android:layout_width="wrap_content"
 44             android:layout_height="wrap_content"
 45             android:layout_alignLeft="@id/logo_img"
 46             android:layout_below="@id/progress_dialog"
 47             android:layout_marginLeft="15dp"
 48             android:text="A basic message"
 49             android:textColor="#797979"
 50             android:textSize="14sp" />
 51 
 52         <Button
 53             android:id="@+id/basic_test"
 54             style="@style/dialog_blue_button"
 55             android:layout_below="@id/txt_1"
 56             android:layout_centerHorizontal="true"
 57             android:layout_margin="10dp"
 58             android:text="Try me!" />
 59 
 60         <TextView
 61             android:id="@+id/txt_2"
 62             android:layout_width="wrap_content"
 63             android:layout_height="wrap_content"
 64             android:layout_alignLeft="@id/logo_img"
 65             android:layout_below="@id/basic_test"
 66             android:layout_marginLeft="15dp"
 67             android:layout_marginTop="15dp"
 68             android:text="A title with a text under"
 69             android:textColor="#797979"
 70             android:textSize="14sp" />
 71 
 72         <Button
 73             android:id="@+id/under_text_test"
 74             style="@style/dialog_blue_button"
 75             android:layout_below="@id/txt_2"
 76             android:layout_centerHorizontal="true"
 77             android:layout_margin="10dp"
 78             android:text="Try me!" />
 79 
 80         <TextView
 81             android:id="@+id/txt_3"
 82             android:layout_width="wrap_content"
 83             android:layout_height="wrap_content"
 84             android:layout_alignLeft="@id/logo_img"
 85             android:layout_below="@id/under_text_test"
 86             android:layout_marginLeft="15dp"
 87             android:layout_marginTop="15dp"
 88             android:text="show error message"
 89             android:textColor="#797979"
 90             android:textSize="14sp" />
 91 
 92         <Button
 93             android:id="@+id/error_text_test"
 94             style="@style/dialog_blue_button"
 95             android:layout_below="@id/txt_3"
 96             android:layout_centerHorizontal="true"
 97             android:layout_margin="10dp"
 98             android:text="Try me!" />
 99 
100         <TextView
101             android:id="@+id/txt_4"
102             android:layout_width="wrap_content"
103             android:layout_height="wrap_content"
104             android:layout_alignLeft="@id/logo_img"
105             android:layout_below="@id/error_text_test"
106             android:layout_marginLeft="15dp"
107             android:layout_marginTop="15dp"
108             android:text="A success message"
109             android:textColor="#797979"
110             android:textSize="14sp" />
111 
112         <Button
113             android:id="@+id/success_text_test"
114             style="@style/dialog_blue_button"
115             android:layout_below="@id/txt_4"
116             android:layout_centerHorizontal="true"
117             android:layout_margin="10dp"
118             android:text="Try me!" />
119 
120         <TextView
121             android:id="@+id/txt_5"
122             android:layout_width="200dp"
123             android:layout_height="wrap_content"
124             android:layout_alignLeft="@id/logo_img"
125             android:layout_below="@id/success_text_test"
126             android:layout_marginLeft="15dp"
127             android:layout_marginTop="15dp"
128             android:text="A warning message, with a listener bind to the Confirm-button..."
129             android:textColor="#797979"
130             android:textSize="14sp" />
131 
132         <Button
133             android:id="@+id/warning_confirm_test"
134             style="@style/dialog_blue_button"
135             android:layout_below="@id/txt_5"
136             android:layout_centerHorizontal="true"
137             android:layout_margin="10dp"
138             android:text="Try me!" />
139 
140         <TextView
141             android:id="@+id/txt_6"
142             android:layout_width="200dp"
143             android:layout_height="wrap_content"
144             android:layout_alignLeft="@id/logo_img"
145             android:layout_below="@id/warning_confirm_test"
146             android:layout_marginLeft="15dp"
147             android:layout_marginTop="15dp"
148             android:text="A warning message, with listeners bind to Cancel and Confirm button..."
149             android:textColor="#797979"
150             android:textSize="14sp" />
151 
152         <Button
153             android:id="@+id/warning_cancel_test"
154             style="@style/dialog_blue_button"
155             android:layout_below="@id/txt_6"
156             android:layout_centerHorizontal="true"
157             android:layout_margin="10dp"
158             android:text="Try me!" />
159 
160         <TextView
161             android:id="@+id/txt_7"
162             android:layout_width="200dp"
163             android:layout_height="wrap_content"
164             android:layout_alignLeft="@id/logo_img"
165             android:layout_below="@id/warning_cancel_test"
166             android:layout_marginLeft="15dp"
167             android:layout_marginTop="15dp"
168             android:text="A message with a custom icon"
169             android:textColor="#797979"
170             android:textSize="14sp" />
171 
172         <Button
173             android:id="@+id/custom_img_test"
174             style="@style/dialog_blue_button"
175             android:layout_below="@id/txt_7"
176             android:layout_centerHorizontal="true"
177             android:layout_margin="10dp"
178             android:text="Try me!" />
179     </RelativeLayout>
180 
181 </ScrollView>
原文地址:https://www.cnblogs.com/zzw1994/p/5010921.html