📜  如何使用SweetAlert对话框库创建AlertDialog框?

📅  最后修改于: 2021-05-13 15:08:16             🧑  作者: Mango

在本文中,我们将学习如何使用SweetAlert对话框库在应用程序中添加“自定义警报对话框” 。这是一个弹出框,响应于用户的任何操作而出现。 AlertBox在验证时非常有用,可用于显示确认消息。假设如果用户在不保存更改的情况下按了“后退”按钮,则可以使用它作为警告。在支付应用程序中完成交易后,可以向用户显示一个简短的对话框,描述交易的状态。

好处:

  • 它为我们提供了非常好的用户界面,使用户体验非常好。
  • 它使工作变得非常容易,因此可在需要创建整个布局的“自定义对话框”上使用。
  • 它为对话框提供了许多不同类型的布局。

方法:

步骤1:build.gradle文件中添加支持库,并在“依赖项”部分中添加依赖项。该库具有内置的警报对话框,可以直接使用。

dependencies {         
        implementation 'com.github.f0ris.sweetalert:library:1.5.1'         
}

步骤2:现在,在activity_main.xml文件中添加以下代码。该代码在活动中添加了五个按钮。每个按钮用于调用警报对话框的不同样式。

activity_main.xml


  
    


MainActivity.java
package org.geeksforgeeks.gfgbottomnav;
  
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import cn.pedant.SweetAlert.SweetAlertDialog;
  
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
    public void showDialog(View view){
        switch (view.getId()){
            case R.id.basic_dialog:
                new SweetAlertDialog(this)
                        .setTitleText("Here's a message!")
                        .setContentText("This is Basic Dialog")
                        .show();
                break;
            case R.id.error_dialog:
                new SweetAlertDialog(
                         this, SweetAlertDialog.ERROR_TYPE)
                        .setTitleText("Oops...")
                        .setContentText("Something went wrong!")
                        .show();
                break;
            case R.id.warning_dialog:
                new SweetAlertDialog(
                         this, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("Are you sure?")
                        .setContentText("Won't be able
                          to recover this file!")
                        .setConfirmText("Yes, delete it!")
                        .show();
                break;
            case R.id.success_dialog:
                new SweetAlertDialog(
                         this, SweetAlertDialog.SUCCESS_TYPE)
                        .setTitleText("Great!")
                        .setContentText("You completed this task.")
                        .show();
                break;
            case R.id.custom_dialog:
                new SweetAlertDialog(
                         this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
                        .setTitleText("Android")
                        .setContentText("Thi is custom dialog")
                        .setCustomImage(R.drawable.ic_android_black)
                        .show();
                break;
        }
    }
}


步骤3:MainActivity中添加以下代码。 Java文件。现在单击任何按钮,都会启动showDialog()函数,并显示相应的“警告对话框” 。也可以将onClickListner添加到按钮中,而不是使用showDialog函数。

主要活动。Java

package org.geeksforgeeks.gfgbottomnav;
  
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import cn.pedant.SweetAlert.SweetAlertDialog;
  
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
    public void showDialog(View view){
        switch (view.getId()){
            case R.id.basic_dialog:
                new SweetAlertDialog(this)
                        .setTitleText("Here's a message!")
                        .setContentText("This is Basic Dialog")
                        .show();
                break;
            case R.id.error_dialog:
                new SweetAlertDialog(
                         this, SweetAlertDialog.ERROR_TYPE)
                        .setTitleText("Oops...")
                        .setContentText("Something went wrong!")
                        .show();
                break;
            case R.id.warning_dialog:
                new SweetAlertDialog(
                         this, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("Are you sure?")
                        .setContentText("Won't be able
                          to recover this file!")
                        .setConfirmText("Yes, delete it!")
                        .show();
                break;
            case R.id.success_dialog:
                new SweetAlertDialog(
                         this, SweetAlertDialog.SUCCESS_TYPE)
                        .setTitleText("Great!")
                        .setContentText("You completed this task.")
                        .show();
                break;
            case R.id.custom_dialog:
                new SweetAlertDialog(
                         this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
                        .setTitleText("Android")
                        .setContentText("Thi is custom dialog")
                        .setCustomImage(R.drawable.ic_android_black)
                        .show();
                break;
        }
    }
}

输出:

参考链接:https://github.com/F0RIS/sweet-alert-dialog

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!