📜  Android中使用Alerter库实现不同类型的AlertBox

📅  最后修改于: 2022-05-13 01:55:27.015000             🧑  作者: Mango

Android中使用Alerter库实现不同类型的AlertBox

在本文中,我们将实现一个与 AlertBox 相关的非常重要的功能。通常,我们创建一个 AlertBox 来显示一些重要的内容。在这里,我们将学习如何使用警报库在我们的 Android 应用程序中实现该功能。下面给出了一个示例视频,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。

第 2 步:添加依赖项和 JitPack 存储库

导航到Gradle Scripts > build.gradle(Module:app)并在依赖项部分添加以下依赖项。



将 JitPack 存储库添加到您的构建文件中。将其添加到 allprojects{} 部分中存储库末尾的根 build.gradle 中。

添加此依赖项后同步您的项目,现在我们将转向它的实现。

步骤 3:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。

XML


  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
      


Java
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.tapadoo.alerter.Alerter;
import com.tapadoo.alerter.OnHideAlertListener;
import com.tapadoo.alerter.OnShowAlertListener;
  
public class MainActivity extends AppCompatActivity {
  
    TextView txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, txt9, txt10;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        txt1 = findViewById(R.id.txt1);
        txt2 = findViewById(R.id.txt2);
        txt3 = findViewById(R.id.txt3);
        txt4 = findViewById(R.id.txt4);
        txt5 = findViewById(R.id.txt5);
        txt6 = findViewById(R.id.txt6);
        txt7 = findViewById(R.id.txt7);
        txt8 = findViewById(R.id.txt8);
        txt9 = findViewById(R.id.txt9);
        txt10 = findViewById(R.id.txt10);
          
        // click on text to show alert
        txt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simpleAlert();
            }
        });
          
        // click on text to show alert
        txt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withBackgroundColor();
            }
        });
          
        // click on text to show alert
        txt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withIcon();
            }
        });
          
        // click on text to show alert
        txt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withOnScreenDuration();
            }
        });
          
        // click on text to show alert
        txt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withoutTitle();
            }
        });
          
        // click on text to show alert
        txt6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withOnClickListener();
            }
        });
          
        // click on text to show alert
        txt7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withVerboseText();
            }
        });
          
        // click on text to show alert
        txt8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withSwipeToDismiss();
            }
        });
          
        // click on text to show alert
        txt9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withProgressBar();
            }
        });
          
        // click on text to show alert
        txt10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withVisibilityCallbacks();
            }
        });
    }
  
    // show simple alert
    public void simpleAlert() {
        Alerter.create(MainActivity.this).setTitle("Geeks For Geeks")
                .setText("A portal for Computer Science Student").show();
    }
  
    // set background color using setBackgroundColorRes
    public void withBackgroundColor() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // or setBackgroundColorInt(Color.CYAN)
                .setText("A portal for Computer Science Student").setBackgroundColorRes(R.color.purple_200) 
                .show();
    }
  
    // without title
    public void withoutTitle() {
        Alerter.create(this)
                .setText("A portal for Computer Science Student").show();
  
    }
      
    public void withOnClickListener() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                .setText("A portal for Computer Science Student").setDuration(10000).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // showing simple toast to user when clicked
                Toast.makeText(MainActivity.this, "You Clicked Me", Toast.LENGTH_LONG).show();
            }
        }).show();
    }
  
    public void withVerboseText() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                .setText("A portal for Computer Science Student"
                        + "A portal for Computer Science Student" + "A portal for Computer Science Student").show();
    }
  
    // set icon using setIcon
    public void withIcon() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // Optional - Removes white tint
                .setText("A portal for Computer Science Student").setIcon(R.drawable.ic_launcher_foreground).setIconColorFilter(0) 
                .show();
    }
  
    // set screen duration using setDuration
    public void withOnScreenDuration() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // set the duration in milli seconds
                .setText("A portal for Computer Science Student").setDuration(10000) 
                .show();
    }
  
    // swipe toast to dismiss
    public void withSwipeToDismiss() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // enabled swipe to dismiss
                .setText("A portal for Computer Science Student").enableSwipeToDismiss() 
                .show();
    }
  
    // show progress bar
    public void withProgressBar() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // enables the progress
                .setText("A portal for Computer Science Student").enableProgress(true) 
                  // set color of the progress
                .setProgressColorRes(R.color.purple_200) 
                .show();
    }
  
    public void withVisibilityCallbacks() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                .setText("A portal for Computer Science Student").setDuration(10000).setOnShowListener(new OnShowAlertListener() {
            @Override
            public void onShow() {
                // showing simple toast when it is shown
                Toast.makeText(MainActivity.this, "On Show ", Toast.LENGTH_SHORT).show();
            }
        }).setOnHideListener(new OnHideAlertListener() {
            @Override
            public void onHide() {
                // showing simple toast to user when  it is hidden
                Toast.makeText(MainActivity.this, "On Hide ", Toast.LENGTH_SHORT).show();
  
            }
        }).show();
    }
}


第 4 步:使用MainActivity。 Java文件

转到主活动。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.tapadoo.alerter.Alerter;
import com.tapadoo.alerter.OnHideAlertListener;
import com.tapadoo.alerter.OnShowAlertListener;
  
public class MainActivity extends AppCompatActivity {
  
    TextView txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, txt9, txt10;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        txt1 = findViewById(R.id.txt1);
        txt2 = findViewById(R.id.txt2);
        txt3 = findViewById(R.id.txt3);
        txt4 = findViewById(R.id.txt4);
        txt5 = findViewById(R.id.txt5);
        txt6 = findViewById(R.id.txt6);
        txt7 = findViewById(R.id.txt7);
        txt8 = findViewById(R.id.txt8);
        txt9 = findViewById(R.id.txt9);
        txt10 = findViewById(R.id.txt10);
          
        // click on text to show alert
        txt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simpleAlert();
            }
        });
          
        // click on text to show alert
        txt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withBackgroundColor();
            }
        });
          
        // click on text to show alert
        txt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withIcon();
            }
        });
          
        // click on text to show alert
        txt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withOnScreenDuration();
            }
        });
          
        // click on text to show alert
        txt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withoutTitle();
            }
        });
          
        // click on text to show alert
        txt6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withOnClickListener();
            }
        });
          
        // click on text to show alert
        txt7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withVerboseText();
            }
        });
          
        // click on text to show alert
        txt8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withSwipeToDismiss();
            }
        });
          
        // click on text to show alert
        txt9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withProgressBar();
            }
        });
          
        // click on text to show alert
        txt10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withVisibilityCallbacks();
            }
        });
    }
  
    // show simple alert
    public void simpleAlert() {
        Alerter.create(MainActivity.this).setTitle("Geeks For Geeks")
                .setText("A portal for Computer Science Student").show();
    }
  
    // set background color using setBackgroundColorRes
    public void withBackgroundColor() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // or setBackgroundColorInt(Color.CYAN)
                .setText("A portal for Computer Science Student").setBackgroundColorRes(R.color.purple_200) 
                .show();
    }
  
    // without title
    public void withoutTitle() {
        Alerter.create(this)
                .setText("A portal for Computer Science Student").show();
  
    }
      
    public void withOnClickListener() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                .setText("A portal for Computer Science Student").setDuration(10000).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // showing simple toast to user when clicked
                Toast.makeText(MainActivity.this, "You Clicked Me", Toast.LENGTH_LONG).show();
            }
        }).show();
    }
  
    public void withVerboseText() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                .setText("A portal for Computer Science Student"
                        + "A portal for Computer Science Student" + "A portal for Computer Science Student").show();
    }
  
    // set icon using setIcon
    public void withIcon() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // Optional - Removes white tint
                .setText("A portal for Computer Science Student").setIcon(R.drawable.ic_launcher_foreground).setIconColorFilter(0) 
                .show();
    }
  
    // set screen duration using setDuration
    public void withOnScreenDuration() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // set the duration in milli seconds
                .setText("A portal for Computer Science Student").setDuration(10000) 
                .show();
    }
  
    // swipe toast to dismiss
    public void withSwipeToDismiss() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // enabled swipe to dismiss
                .setText("A portal for Computer Science Student").enableSwipeToDismiss() 
                .show();
    }
  
    // show progress bar
    public void withProgressBar() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                // enables the progress
                .setText("A portal for Computer Science Student").enableProgress(true) 
                  // set color of the progress
                .setProgressColorRes(R.color.purple_200) 
                .show();
    }
  
    public void withVisibilityCallbacks() {
        Alerter.create(this).setTitle("Geeks For Geeks")
                .setText("A portal for Computer Science Student").setDuration(10000).setOnShowListener(new OnShowAlertListener() {
            @Override
            public void onShow() {
                // showing simple toast when it is shown
                Toast.makeText(MainActivity.this, "On Show ", Toast.LENGTH_SHORT).show();
            }
        }).setOnHideListener(new OnHideAlertListener() {
            @Override
            public void onHide() {
                // showing simple toast to user when  it is hidden
                Toast.makeText(MainActivity.this, "On Hide ", Toast.LENGTH_SHORT).show();
  
            }
        }).show();
    }
}

输出: