封装AlertDialog工具类

自定义两个按钮弹框和一个按钮的弹框
封装AlertDialog工具类_第1张图片
直接上代码,AlertDialogUtil工具类如下:

public class AlertDialogUtil {

    /**
     * @param context        上下文
     * @param title          标题
     * @param content        内容
     * @param btnCancleText  取消按钮文本
     * @param btnSureText    确定按钮文本
     * @param cancleListener 取消监听
     * @param sureListener   确定监听
     * @return
     */
    public synchronized static AlertDialog showDialog(Context context, String title, String content,
        String btnCancleText, String btnSureText, DialogInterface.OnClickListener cancleListener,
        DialogInterface.OnClickListener sureListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        AlertDialog dialog = builder.create();
       // dialog弹出后会点击屏幕,dialog不消失;点击物理返回键dialog消失
        dialog.setCanceledOnTouchOutside(false);
        //dialog弹出后会点击屏幕或物理返回键,dialog不消失
        dialog.setCancelable(false);

        View view = View.inflate(context, R.layout.dialog, null);
        //标题
        TextView tvTitle = view.findViewById(R.id.tv_alert_title);
        //内容
        TextView tvContent = view.findViewById(R.id.tv_alert_content);
        //取消按钮
        TextView buttonCancle = view.findViewById(R.id.tv_dialog_cancel);
        //确定按钮
        TextView buttonOk = view.findViewById(R.id.tv_dialog_ok);
        //view
        View viewLine = view.findViewById(R.id.view_line);

        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }

        tvContent.setText(TextUtils.isEmpty(content) ? "" : content);

        if (TextUtils.isEmpty(btnCancleText)) {
            buttonCancle.setVisibility(View.GONE);
            viewLine.setVisibility(View.GONE);
        } else {
            buttonCancle.setText(btnCancleText);
        }
        if (TextUtils.isEmpty(btnSureText)) {
            buttonOk.setVisibility(View.GONE);
        } else {
            buttonOk.setText(btnSureText);
        }

        final AlertDialog dialogFinal = dialog;
        final DialogInterface.OnClickListener finalCancleListener = cancleListener;
        final DialogInterface.OnClickListener finalSureListener = sureListener;
        buttonCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalCancleListener.onClick(dialogFinal, DialogInterface.BUTTON_NEGATIVE);
            }
        });
        buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalSureListener.onClick(dialogFinal, DialogInterface.BUTTON_POSITIVE);
            }
        });

        //设置背景透明,去四个角
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.show();
        dialog.setContentView(view);
        return dialog;
    }
}

XML布局,如下:




    

        

        

        

            

            

            

            
        
    


所用的shape代码:



    
    
    


调用:
AlertDialogUtil.showDialog(this, xx,xx,xx, , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    //取消
                        dialog.dismiss();
                    }
                }, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //确定逻辑
                    }
                });

你可能感兴趣的:(工具类)