Github项目地址:https://github.com/pedant/sweet-alert-dialog
1、在app的build.gradle中添加:
compile 'com.github.f0ris.sweetalert:library:1.5.1'
mavenCentral()
2、在AndroidManifest.xml中添加:
xmlns:tools="http://schemas.android.com/tools"
tools:replace="icon"
3、创建一个工具类使用:
public class AlertDialogUtils { private Context context; private SweetAlertDialog alertDialog; public AlertDialogUtils(Context context){ this.context = context; } /**【标题 弹框】**/ public void AlertTitle(String title , String type){ if (type.equals("warning")){ alertDialog = new SweetAlertDialog(context , SweetAlertDialog.WARNING_TYPE); }else if (type.equals("error")){ alertDialog = new SweetAlertDialog(context , SweetAlertDialog.ERROR_TYPE); }else if (type.equals("progress")){ alertDialog = new SweetAlertDialog(context , SweetAlertDialog.PROGRESS_TYPE); alertDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86")); alertDialog.setCancelable(true); }else { alertDialog = new SweetAlertDialog(context , SweetAlertDialog.SUCCESS_TYPE); } alertDialog.setTitleText(title); alertDialog.show(); } /**【标题 + 内容 弹框】**/ public void AlertTitleAndMessage(String title , String message , String type){ if (type.equals("warning")){ alertDialog = new SweetAlertDialog(context , SweetAlertDialog.WARNING_TYPE); }else if (type.equals("error")){ alertDialog = new SweetAlertDialog(context , SweetAlertDialog.ERROR_TYPE); }else{ alertDialog = new SweetAlertDialog(context , SweetAlertDialog.SUCCESS_TYPE); } alertDialog.setTitleText(title); alertDialog.setContentText(message); alertDialog.setConfirmText("确定"); alertDialog.show(); } }
4、在Kotlin中调用:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button_Test.setOnClickListener{ val alertUtils = AlertDialogUtils(this) alertUtils.AlertDialogChangeType("测试标题" ,"测试内容","warning") } button_Save.setOnClickListener{ val alertUtils = AlertDialogUtils(this) alertUtils.AlertTitle("测试标题" ,"progress") } }
5、运行结果: