第三方开源对话消息提示框

51 篇文章 0 订阅

效果如下:


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)引用。


SweetAlertDialog 中的 SampleActivity.java :

[java]  view plain copy
  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 * 7800) {  
  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 文件:

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

SweetAlertDialog 导入所依赖的 materialish-progress 库时可能会出错:

需要把 materialish-progress 库中的 values 下的 attrs.xml 中的  matProg_progressIndeterminate 复制到 SweetAlertDialog 库的 alert_dialog.xml 中,如下:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值