Android 对话框sweet-alert-dialog

 更新时间:2016年09月19日 17:00:33   投稿:lqh  
这篇文章主要介绍了Android 对话框sweet-alert-dialog的相关资料,需要的朋友可以参考下

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 Service在手机休眠后继续运行的方法

    保持Android Service在手机休眠后继续运行的方法

    下面小编就为大家分享一篇保持Android Service在手机休眠后继续运行的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • Android提高之多级树形菜单的实现方法

    Android提高之多级树形菜单的实现方法

    这篇文章主要介绍了Android多级树形菜单的实现方法,很实用的功能,需要的朋友可以参考下
    2014-08-08
  • Android选中突出背景效果的底部导航栏功能

    Android选中突出背景效果的底部导航栏功能

    这篇文章主要介绍了Android选中突出背景效果的底部导航栏功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • Android实战之Cocos游戏容器搭建

    Android实战之Cocos游戏容器搭建

    这篇文章主要介绍了Android实战之Cocos游戏容器搭建,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-06-06
  • Android用文件存储数据的方法

    Android用文件存储数据的方法

    这篇文章主要为大家详细介绍了Android用文件存储数据的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Jetpack Compose修饰符专项精讲

    Jetpack Compose修饰符专项精讲

    在今年的Google/IO大会上,亮相了一个全新的 Android 原生 UI 开发框架-Jetpack Compose, 与苹果的SwiftIUI一样,Jetpack Compose是一个声明式的UI框架,它可简化并加快Android上的界面开发,使用更少的代码、强大的工具和直观的 Kotlin API,快速让应用生动而精彩
    2022-10-10
  • Android 自定义Button控件实现按钮点击变色

    Android 自定义Button控件实现按钮点击变色

    这篇文章给大家介绍了android 自定义Button控件实现按钮点击变色的代码,本文给大家附有注释,非常不错,代码简单易懂,对android按钮点击变色的实现感兴趣的朋友参考下吧
    2016-11-11
  • Android编程实现摄像头临摹效果的方法

    Android编程实现摄像头临摹效果的方法

    这篇文章主要介绍了Android编程实现摄像头临摹效果的方法,涉及Android权限控制、布局及摄像头功能调用等相关操作技巧,需要的朋友可以参考下
    2017-09-09
  • 解决 INSTALL FAILED CONFLICTING PROVIDER的问题方法

    解决 INSTALL FAILED CONFLICTING PROVIDER的问题方法

    这篇文章主要介绍了解决 INSTALL FAILED CONFLICTING PROVIDER的问题方法的相关资料,需要的朋友可以参考下
    2017-02-02
  • Kotlin关于协程是什么的探究

    Kotlin关于协程是什么的探究

    Android官方对协程的定义-协程是一种并发设计模式,您可以在Android平台上使用它来简化异步执行的代码。协程是在版本1.3中添加到Kotlin的,它基于来自其他语言的既定概念
    2023-01-01

最新评论