📌  相关文章
📜  如何在android中使用Alerter Library创建弹出消息

📅  最后修改于: 2021-10-28 02:18:15             🧑  作者: Mango

在本文中,我们将学习如何在Alerter Library的帮助下创建弹出消息。如果要向用户显示某些警报消息,则使用Alerter比使用ToastSnackbar更好。我们可以将各种onClickListners添加到我们的警报器消息中,这使它变得更好,并且它还有很好的吸引人的用户界面。


方法:

  1. build.gradle文件中添加支持库并在依赖项部分添加依赖项。该库有助于在应用程序中轻松集成警报视图。警报视图是可自定义的,它显示在应用程序中正在进行的活动上。这个库也与AndroidX兼容。
    dependencies {
        implementation 'com.tapadoo.android:alerter:2.0.4'
    }
    
  2. 现在在activity_main.xml文件中添加以下代码。此代码在 MainActivity 中添加一个按钮,如果单击该按钮,则调用showAlerter函数。
    activity_main.xml
    
    
      
        


    MainActivity.java
    package org.geeksforgeeks.gfgAlerter;
      
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import com.tapadoo.alerter.Alerter;
    import com.tapadoo.alerter.OnHideAlertListener;
    import com.tapadoo.alerter.OnShowAlertListener;
      
    public class MainActivity extends AppCompatActivity {
      
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            button = findViewById(R.id.button);
        }
      
        public void showAlerter(View v)
        {
            Alerter.create(this)
                .setTitle("Alert Title")
                .setText("Alert Text")
                .setIcon(
                    R.drawable.ic_android_black_24dp)
                .setBackgroundColorRes(
                    R.color.colorAccent)
                .setDuration(3000)
                .setOnClickListener(
                    new View.OnClickListener() {
      
                        @Override
                        public void onClick(View v)
                        {
                            // do something when
                            // Alerter message was clicked
                        }
                    })
      
                .setOnShowListener(
                    new OnShowAlertListener() {
      
                        @Override
                        public void onShow()
                        {
                            // do something when
                            // Alerter message shows
                        }
                    })
      
                .setOnHideListener(
                    new OnHideAlertListener() {
      
                        @Override
                        public void onHide()
                        {
                            // do something when
                            // Alerter message hides
                        }
                    })
                .show();
        }
    }


  3. 现在在MainActivity 中添加以下代码。 Java文件。它定义了showAlerter函数。此函数创建警报器。调用各种方法来初始化警报器的不同属性。 setTitle设置标题, setText设置显示在标题下方的文本, setIcon设置图标等。还附加了各种onClickListeners以便您可以响应用户的操作做一些事情。

    主要活动。Java

    package org.geeksforgeeks.gfgAlerter;
      
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import com.tapadoo.alerter.Alerter;
    import com.tapadoo.alerter.OnHideAlertListener;
    import com.tapadoo.alerter.OnShowAlertListener;
      
    public class MainActivity extends AppCompatActivity {
      
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            button = findViewById(R.id.button);
        }
      
        public void showAlerter(View v)
        {
            Alerter.create(this)
                .setTitle("Alert Title")
                .setText("Alert Text")
                .setIcon(
                    R.drawable.ic_android_black_24dp)
                .setBackgroundColorRes(
                    R.color.colorAccent)
                .setDuration(3000)
                .setOnClickListener(
                    new View.OnClickListener() {
      
                        @Override
                        public void onClick(View v)
                        {
                            // do something when
                            // Alerter message was clicked
                        }
                    })
      
                .setOnShowListener(
                    new OnShowAlertListener() {
      
                        @Override
                        public void onShow()
                        {
                            // do something when
                            // Alerter message shows
                        }
                    })
      
                .setOnHideListener(
                    new OnHideAlertListener() {
      
                        @Override
                        public void onHide()
                        {
                            // do something when
                            // Alerter message hides
                        }
                    })
                .show();
        }
    }
    

输出:

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