Android最简便使用漂亮的对话框sweet-alert-dialog

转载请注明原创地址:http://blog.csdn.net/taa1007/article/details/79551928

android原生的dialog太生硬了,之前看到了这个效果非常不错但是没有用过.网上有很多相关的博客,但是介绍啰嗦,用法复杂。今天给大家介绍一种最简单的实现方法。

效果如下

Android最简便使用漂亮的对话框sweet-alert-dialog_第1张图片

Android最简便使用漂亮的对话框sweet-alert-dialog_第2张图片

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

1.首先在app的build.gradle中添加依赖:    

compile 'com.github.f0ris.sweetalert:library:1.5.1'

2.这个时候同步一下项目会报一个错误,解决办法:

    (1)在AndroidManifest.xml中manifest根标签添加    

            xmlns:tools="http://schemas.android.com/tools"

    (2)在application标签下添加

            tools:replace="android:icon,android:theme,android:allowBackup"

3.使用代码:

显示Material进度样式

SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
pDialog.setTitleText("Loading");
pDialog.setCancelable(false);
pDialog.show();

效果图

你可以通过SweetAlertDialog.getProgressHelper()调用materialish-progress中下面这些方法,来动态改变进度条的样式

resetCount()
isSpinning()
spin()
stopSpinning()
getProgress()
setProgress(float progress)
setInstantProgress(float progress)
getCircleRadius()
setCircleRadius(int circleRadius)
getBarWidth()
setBarWidth(int barWidth)
getBarColor()
setBarColor(int barColor)
getRimWidth()
setRimWidth(int rimWidth)
getRimColor()
setRimColor(int rimColor)
getSpinSpeed()
setSpinSpeed(float spinSpeed)

只显示标题:

new SweetAlertDialog(this)
    .setTitleText("Here's a message!")
    .show();
显示标题和内容:

new SweetAlertDialog(this)
    .setTitleText("Here's a message!")
    .setContentText("It's pretty, isn't it?")
    .show();
显示异常样式:

new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
    .setTitleText("Oops...")
    .setContentText("Something went wrong!")
    .show();
显示警告样式:

new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
    .setTitleText("Are you sure?")
    .setContentText("Won't be able to recover this file!")
    .setConfirmText("Yes,delete it!")
    .show();
显示成功完成样式:

new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE)
    .setTitleText("Good job!")
    .setContentText("You clicked the button!")
    .show();
自定义头部图像:

new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
    .setTitleText("Sweet!")
    .setContentText("Here's a custom image.")
    .setCustomImage(R.drawable.custom_img)
    .show();
确认事件绑定:

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) {
            sDialog.dismissWithAnimation();
        }
    })
    .show();
显示取消按钮及事件绑定:

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) {
            sDialog.cancel();
        }
    })
    .show();
确认后切换对话框样式:

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) {
            sDialog
                .setTitleText("Deleted!")
                .setContentText("Your imaginary file has been deleted!")
                .setConfirmText("OK")
                .setConfirmClickListener(null)
                .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
        }
    })
    .show();
今天的介绍就到这里了,这个漂亮的dialog你学会了吗?

你可能感兴趣的:(Android最简便使用漂亮的对话框sweet-alert-dialog)