• 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();




  • 相关阅读:
    [Bullet3]创建世界(场景)及常见函数
    [erlang]supervisor(监控树)的重启策略
    [game]十字链表的AOI算法实现
    [翻译][erlang]cowboy handler模块的使用
    数据挖掘算法系列目录
    Spark原理分析目录
    Spark实战系列目录
    2019年读书书单
    Hadoop源码解读系列目录
    分布式架构系列目录
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779744.html
Copyright © 2020-2023  润新知