Android 对话框sweet-alert-dialog

所属分类: 软件编程 / Android 阅读数: 27
收藏 0 赞 0 分享

android原生的dialog太生硬了,之前看到了这个效果非常不错但是没有用过,今天给别人推荐使用,他遇到了问题,导入后错误非常多,也没有库工程。于是自己认真看了一下,这是个AndroidStudio的工程,并且里面还依赖于materialish-progress工程,也是个AS的工程。于是打算弄一个eclipse的版本并且将这两个工程融合在一起作为一个库工程XAlertDialogLibrary。使用时将其作为库导入项目中即可。

效果如下

使用起来非常简单,测试代码如下:

MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener {
 
  private int i = -1;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.basic_test).setOnClickListener(this);
    findViewById(R.id.under_text_test).setOnClickListener(this);
    findViewById(R.id.error_text_test).setOnClickListener(this);
    findViewById(R.id.success_text_test).setOnClickListener(this);
    findViewById(R.id.warning_confirm_test).setOnClickListener(this);
    findViewById(R.id.warning_cancel_test).setOnClickListener(this);
    findViewById(R.id.custom_img_test).setOnClickListener(this);
    findViewById(R.id.progress_dialog).setOnClickListener(this);
  }
 
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.basic_test:
        // default title "Here's a message!"
        SweetAlertDialog sd = new SweetAlertDialog(this);
        sd.setCancelable(true);
        sd.setCanceledOnTouchOutside(true);
        sd.show();
        break;
      case R.id.under_text_test:
        new SweetAlertDialog(this)
            .setContentText("It's pretty, isn't it?")
            .show();
        break;
      case R.id.error_text_test:
        new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
            .setTitleText("Oops...")
            .setContentText("Something went wrong!")
            .show();
        break;
      case R.id.success_text_test:
        new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE)
            .setTitleText("Good job!")
            .setContentText("You clicked the button!")
            .show();
        break;
      case R.id.warning_confirm_test:
        new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
            .setTitleText("Are you sure?")
            .setContentText("Won't be able to recover this file!")
            .setConfirmText("Yes,delete it!")
            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sDialog) {
              // reuse previous dialog instance
              sDialog.setTitleText("Deleted!")
                  .setContentText("Your imaginary file has been deleted!")
                  .setConfirmText("OK")
                  .setConfirmClickListener(null)
                  .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
            }
            })
            .show();
        break;
      case R.id.warning_cancel_test:
        new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
            .setTitleText("Are you sure?")
            .setContentText("Won't be able to recover this file!")
            .setCancelText("No,cancel plx!")
            .setConfirmText("Yes,delete it!")
            .showCancelButton(true)
            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
              @Override
              public void onClick(SweetAlertDialog sDialog) {
                // reuse previous dialog instance, keep widget user state, reset them if you need
                sDialog.setTitleText("Cancelled!")
                    .setContentText("Your imaginary file is safe :)")
                    .setConfirmText("OK")
                    .showCancelButton(false)
                    .setCancelClickListener(null)
                    .setConfirmClickListener(null)
                    .changeAlertType(SweetAlertDialog.ERROR_TYPE);
 
                // or you can new a SweetAlertDialog to show
                /* sDialog.dismiss();
                new SweetAlertDialog(SampleActivity.this, SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("Cancelled!")
                    .setContentText("Your imaginary file is safe :)")
                    .setConfirmText("OK")
                    .show();*/
              }
            })
            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
              @Override
              public void onClick(SweetAlertDialog sDialog) {
                sDialog.setTitleText("Deleted!")
                    .setContentText("Your imaginary file has been deleted!")
                    .setConfirmText("OK")
                    .showCancelButton(false)
                    .setCancelClickListener(null)
                    .setConfirmClickListener(null)
                    .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
              }
            })
            .show();
        break;
      case R.id.custom_img_test:
        new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
            .setTitleText("Sweet!")
            .setContentText("Here's a custom image.")
            .setCustomImage(R.drawable.custom_img)
            .show();
        break;
      case R.id.progress_dialog:
        final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
            .setTitleText("Loading");
        pDialog.show();
        pDialog.setCancelable(false);
        new CountDownTimer(800 * 7, 800) {
          public void onTick(long millisUntilFinished) {
            // you can change the progress bar color by ProgressHelper every 800 millis
            i++;
            switch (i){
              case 0:
                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
                break;
              case 1:
                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50));
                break;
              case 2:
                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                break;
              case 3:
                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20));
                break;
              case 4:
                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80));
                break;
              case 5:
                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color));
                break;
              case 6:
                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                break;
            }
          }
 
          public void onFinish() {
            i = -1;
            pDialog.setTitleText("Success!")
                .setConfirmText("OK")
                .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
          }
        }.start();
        break;
    }
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#FFF"
   xmlns:android="http://schemas.android.com/apk/res/android">
 
   <RelativeLayout android:layout_width="match_parent"
      android:paddingBottom="10dp"
      android:layout_height="wrap_content">
 
      <ImageView
         android:id="@+id/logo_img"
         android:layout_width="180dp"
         android:layout_height="wrap_content"
         android:src="@drawable/logo_big"
         android:layout_marginTop="10dp"
         android:layout_marginBottom="15dp"
         android:layout_centerHorizontal="true"
         android:contentDescription="@string/app_name"/>
 
         <TextView
            android:id="@+id/txt_0"
            android:layout_alignLeft="@id/logo_img"
            android:layout_below="@id/logo_img"
            android:layout_marginLeft="15dp"
            android:text="show material progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="#797979"/>
 
         <Button
            android:layout_centerHorizontal="true"
            android:layout_below="@id/txt_0"
            android:id="@+id/progress_dialog"
            style="@style/dialog_blue_button"
            android:layout_margin="10dp"
            android:text="Try me!"/>
 
        <TextView
            android:id="@+id/txt_1"
            android:layout_alignLeft="@id/logo_img"
            android:layout_below="@id/progress_dialog"
            android:layout_marginLeft="15dp"
            android:text="A basic message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="#797979"/>
 
        <Button
            android:layout_centerHorizontal="true"
            android:layout_below="@id/txt_1"
            android:id="@+id/basic_test"
            style="@style/dialog_blue_button"
            android:layout_margin="10dp"
            android:text="Try me!"/>
 
       <TextView
           android:id="@+id/txt_2"
           android:layout_alignLeft="@id/logo_img"
           android:layout_below="@id/basic_test"
           android:layout_marginLeft="15dp"
           android:text="A title with a text under"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="14sp"
           android:layout_marginTop="15dp"
           android:textColor="#797979"/>
 
       <Button
           android:layout_centerHorizontal="true"
           android:layout_below="@id/txt_2"
           android:id="@+id/under_text_test"
           style="@style/dialog_blue_button"
           android:layout_margin="10dp"
           android:text="Try me!"/>
 
       <TextView
           android:id="@+id/txt_3"
           android:layout_alignLeft="@id/logo_img"
           android:layout_below="@id/under_text_test"
           android:layout_marginLeft="15dp"
           android:text="show error message"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="14sp"
           android:layout_marginTop="15dp"
           android:textColor="#797979"/>
 
       <Button
          android:layout_centerHorizontal="true"
          android:layout_below="@id/txt_3"
          android:id="@+id/error_text_test"
          style="@style/dialog_blue_button"
          android:layout_margin="10dp"
          android:text="Try me!"/>
 
       <TextView
          android:id="@+id/txt_4"
          android:layout_alignLeft="@id/logo_img"
          android:layout_below="@id/error_text_test"
          android:layout_marginLeft="15dp"
          android:text="A success message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="14sp"
          android:layout_marginTop="15dp"
          android:textColor="#797979"/>
 
       <Button
          android:layout_centerHorizontal="true"
          android:layout_below="@id/txt_4"
          android:id="@+id/success_text_test"
          style="@style/dialog_blue_button"
          android:layout_margin="10dp"
          android:text="Try me!"/>
 
       <TextView
          android:id="@+id/txt_5"
          android:layout_alignLeft="@id/logo_img"
          android:layout_below="@id/success_text_test"
          android:layout_marginLeft="15dp"
          android:text="A warning message, with a listener bind to the Confirm-button..."
          android:layout_width="200dp"
          android:layout_height="wrap_content"
          android:textSize="14sp"
          android:layout_marginTop="15dp"
          android:textColor="#797979"/>
 
      <Button
          android:layout_centerHorizontal="true"
          android:layout_below="@id/txt_5"
          android:id="@+id/warning_confirm_test"
          style="@style/dialog_blue_button"
          android:layout_margin="10dp"
          android:text="Try me!"/>
 
      <TextView
          android:id="@+id/txt_6"
          android:layout_alignLeft="@id/logo_img"
          android:layout_below="@id/warning_confirm_test"
          android:layout_marginLeft="15dp"
          android:text="A warning message, with listeners bind to Cancel and Confirm button..."
          android:layout_width="200dp"
          android:layout_height="wrap_content"
          android:textSize="14sp"
          android:layout_marginTop="15dp"
          android:textColor="#797979"/>
 
      <Button
         android:layout_centerHorizontal="true"
         android:layout_below="@id/txt_6"
         android:id="@+id/warning_cancel_test"
         style="@style/dialog_blue_button"
         android:layout_margin="10dp"
         android:text="Try me!"/>
 
      <TextView
         android:id="@+id/txt_7"
         android:layout_alignLeft="@id/logo_img"
         android:layout_below="@id/warning_cancel_test"
         android:layout_marginLeft="15dp"
         android:text="A message with a custom icon"
         android:layout_width="200dp"
         android:layout_height="wrap_content"
         android:textSize="14sp"
         android:layout_marginTop="15dp"
         android:textColor="#797979"/>
 
     <Button
         android:layout_centerHorizontal="true"
         android:layout_below="@id/txt_7"
         android:id="@+id/custom_img_test"
         style="@style/dialog_blue_button"
         android:layout_margin="10dp"
         android:text="Try me!"/>
 
   </RelativeLayout>
</ScrollView>

XAlertDialogLibrary(eclipse):点此下载

以上就是Android 对话框sweet-alert-dialog 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多