Android Dialog 简单封装

转载:https://www.cnblogs.com/zjjne/archive/2013/10/03/3350382.html

public class MyAlertDialog {


    //region 确认/取消 弹出框
    //取消按钮,默认canel
    public static Dialog createConfirmDialog(Context context, String title, String message,
                                             String positiveBtnName, String negativeBtnName, DialogInterface.OnClickListener positiveBtnListener) {
        Dialog dialog = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        //设置对话框标题
        builder.setTitle(title);
        //设置对话框消息
        builder.setMessage(message);
        //设置确定按钮
        builder.setPositiveButton(positiveBtnName, positiveBtnListener);
        //设置取消按钮
        builder.setNegativeButton(negativeBtnName, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        //创建一个消息对话框
        dialog = builder.create();

        return dialog;
    }

    //自定义取消按钮事件
    public static Dialog createConfirmDialog(Context context, String title, String message,
                                             String positiveBtnName, String negativeBtnName, DialogInterface.OnClickListener positiveBtnListener,
                                             DialogInterface.OnClickListener negativeBtnListener) {
        Dialog dialog = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        //设置对话框标题
        builder.setTitle(title);
        //设置对话框消息
        builder.setMessage(message);
        //设置确定按钮
        builder.setPositiveButton(positiveBtnName, positiveBtnListener);
        //设置取消按钮
        builder.setNegativeButton(negativeBtnName, negativeBtnListener);
        //创建一个消息对话框
        dialog = builder.create();

        return dialog;
    }
    //endregion


    //region 单选 弹出框

    public static Dialog createRadioDialog(Context context, String title, final String[] ss , DialogInterface.OnClickListener btnListener) {
        Dialog dialog = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        //设置对话框标题
        builder.setTitle(title);
        builder.setSingleChoiceItems(ss, 1, btnListener);

        //创建一个消息对话框
        dialog = builder.create();

        return dialog;
    }

    //endregion

}


调用方式:

点击btn按钮时,弹出对话框。

确认后,执行你的方法();


调用确认框

btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Dialog dialog = MyAlertDialog.createConfirmDialog(InboundPOActivity.this, "提交", "入库确认", "确定", "取消",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                你的方法();
                            }
                        });
                dialog.show();
            }
        });


调用单选框

final String[] ss={"1","2","3"};
                Dialog dialog = MyAlertDialog.createRadioDialog(InboundPOActivity.this,"Test",ss,new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(InboundPOActivity.this, "性别为:" + ss[which], Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.show();




原文地址:https://www.cnblogs.com/hanjun0612/p/9779744.html