最近在做Android项目的时候总觉得系统提供的Alert弹出框代码过于繁琐, 因此对系统Alert的代码进行了二次封装,这样以后开发的时候再也不用写很多代码了。 代码如下:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
/**
* Alert工具类
*
* @author Samoy
* @date 2018/1/10
*/
public final class AlertUtils {
private static AlertDialog dialog;
public static void showProgress(Activity activity, String title, String message) {
if (dialog != null) {
dismiss();
}
dialog = ProgressDialog.show(activity, title, message, false, false);
}
public static void dismiss() {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}
public static void showAlert(Activity activity, String title, String message) {
if (dialog != null) {
dismiss();
}
dialog = new AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setPositiveButton("确定", null)
.show();
}
public static void showAlert(Activity activity, String title, String message, DialogInterface.OnClickListener onEnsureClick) {
if (dialog != null) {
dismiss();
}
dialog = new AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setPositiveButton("确定", onEnsureClick)
.show();
}
public static void showAlert(Activity activity, String title, String message, DialogInterface.OnClickListener onEnsureClick, DialogInterface.OnClickListener onCancelClick) {
if (dialog != null) {
dismiss();
}
dialog = new AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setPositiveButton("确定", onEnsureClick)
.setNegativeButton("取消", onCancelClick)
.show();
}
public static void showAlert(Activity activity, String title, String message, DialogInterface.OnClickListener onEnsureClick, DialogInterface.OnClickListener onCancelClick, DialogInterface.OnClickListener onNeutralClick) {
if (dialog != null) {
dismiss();
}
dialog = new AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setPositiveButton("确定", onEnsureClick)
.setNegativeButton("取消", onCancelClick)
.setNeutralButton("以后再说", onNeutralClick)
.show();
}
}
后记:单项和多项选择以后再实现,这样的需求不是很多。
欢迎在评论区留下您的见解~