Android漂亮的對話框項目sweet-alert-dialog

轉自http://blog.csdn.net/cauchyweierstrass/article/details/46335143

漂亮的對話框 sweet-alert-dialog

項目地址: https://github.com/pedant/sweet-alert-dialog

android原生的dialog太生硬了,之前看到了這個效果非常不錯但是沒有用過,今天給別人推薦使用,他遇到了問題,導入後錯誤非常多,也沒有庫工程。於是自己認真看了一下,這是個AndroidStudio的工程,並且裏面還依賴於materialish-progress工程,也是個AS的工程。於是打算弄一個eclipse的版本並且將這兩個工程融合在一起作爲一個庫工程XAlertDialogLibrary。使用時將其作爲庫導入項目中即可。

效果如下



使用起來非常簡單,測試代碼如下:

MainActivity.java

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

activity_main.xml

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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>  


XAlertDialogLibrary(eclipse):點此下載
發佈了45 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章