• Android 常用对话框Dialog封装



    Android 6种 常用对话框Dialog封装

    包括:

    消息对话框、警示(含确认、取消)对话框、单选对话框、

    复选对话框、列表对话框、自定义视图(含确认、取消)对话框

    分别如下图所示:

    消息对话框 警示(确认、取消)对话框 单选对话框

     复选对话框 列表对话框 自定义视图(含确认、取消)对话框

    封装后代码:

    package dialog;
    
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface.OnClickListener;
    import android.content.DialogInterface.OnMultiChoiceClickListener;
    import android.view.View;
    
    
    
    /**
     * 对话框封装类
     * 
     * @author Z
     *
     */
    public class DialogTool
    {
        
        public static final int NO_ICON = -1;  //无图标
        
        
        
        /**
         * 创建消息对话框
         * 
         * @param context 上下文 必填
         * @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填
         * @param title 标题 必填
         * @param message 显示内容 必填
         * @param btnName 按钮名称 必填
         * @param listener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @return
         */
        public static Dialog createMessageDialog(Context context, String title, String message,
                String btnName, OnClickListener listener, int iconId)
        {
            Dialog dialog = null;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            
            if (iconId != NO_ICON)
            {
                //设置对话框图标
                builder.setIcon(iconId);
            }
            //设置对话框标题
            builder.setTitle(title);
            //设置对话框消息
            builder.setMessage(message);
            //设置按钮
            builder.setPositiveButton(btnName, listener);
            //创建一个消息对话框
            dialog = builder.create();
            
            return dialog;
        }
        
        
        
        /**
         * 创建警示(确认、取消)对话框
         * 
         * @param context 上下文 必填
         * @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填
         * @param title 标题 必填
         * @param message 显示内容 必填
         * @param positiveBtnName 确定按钮名称 必填
         * @param negativeBtnName 取消按钮名称 必填
         * @param positiveBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @return
         */
        public static Dialog createConfirmDialog(Context context, String title, String message,
                String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, 
                OnClickListener negativeBtnListener, int iconId)
        {
            Dialog dialog = null;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            
            if (iconId != NO_ICON)
            {
                //设置对话框图标
                builder.setIcon(iconId);
            }
            //设置对话框标题
            builder.setTitle(title);
            //设置对话框消息
            builder.setMessage(message);
            //设置确定按钮
            builder.setPositiveButton(positiveBtnName, positiveBtnListener);
            //设置取消按钮
            builder.setNegativeButton(negativeBtnName, negativeBtnListener);
            //创建一个消息对话框
            dialog = builder.create();
            
            return dialog;
        }
        
        
        
        /**
         * 创建单选对话框
         * 
         * @param context 上下文 必填
         * @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填
         * @param title 标题 必填
         * @param itemsString 选择项 必填
         * @param positiveBtnName 确定按钮名称 必填
         * @param negativeBtnName 取消按钮名称 必填
         * @param positiveBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @param itemClickListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @return
         */
        public static Dialog createSingleChoiceDialog(Context context, String title, String[] itemsString,
                String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, 
                OnClickListener negativeBtnListener, OnClickListener itemClickListener, int iconId)
        {
            Dialog dialog = null;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            
            if (iconId != NO_ICON)
            {
                //设置对话框图标
                builder.setIcon(iconId);
            }
            //设置对话框标题
            builder.setTitle(title);
            //设置单选选项, 参数0: 默认第一个单选按钮被选中
            builder.setSingleChoiceItems(itemsString, 0, itemClickListener);
            //设置确定按钮
            builder.setPositiveButton(positiveBtnName, positiveBtnListener);
            //设置确定按钮
            builder.setNegativeButton(negativeBtnName, negativeBtnListener);
            //创建一个消息对话框
            dialog = builder.create();
            
            return dialog;
        }
        
        
        
        /**
         * 创建复选对话框
         * 
         * @param context 上下文 必填
         * @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填
         * @param title 标题 必填
         * @param itemsString 选择项 必填
         * @param positiveBtnName 确定按钮名称 必填
         * @param negativeBtnName 取消按钮名称 必填
         * @param positiveBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @param itemClickListener 监听器,需实现android.content.DialogInterface.OnMultiChoiceClickListener;接口 必填
         * @return
         */
        public static Dialog createMultiChoiceDialog(Context context, String title, String[] itemsString,
                String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, 
                OnClickListener negativeBtnListener, OnMultiChoiceClickListener itemClickListener, int iconId)
        {
            Dialog dialog = null;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            
            if (iconId != NO_ICON)
            {
                //设置对话框图标
                builder.setIcon(iconId);
            }
            //设置对话框标题
            builder.setTitle(title);
            //设置选项
            builder.setMultiChoiceItems(itemsString, null, itemClickListener);
            //设置确定按钮
            builder.setPositiveButton(positiveBtnName, positiveBtnListener);
            //设置确定按钮
            builder.setNegativeButton(negativeBtnName, negativeBtnListener);
            //创建一个消息对话框
            dialog = builder.create();
            
            return dialog;
        }
        
        
        
        /**
         * 创建列表对话框
         * 
         * @param context 上下文 必填
         * @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填
         * @param title 标题 必填
         * @param itemsString 列表项 必填
         * @param negativeBtnName 取消按钮名称 必填
         * @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @return
         */
        public static Dialog createListDialog(Context context, String title, String[] itemsString, 
                String negativeBtnName, OnClickListener negativeBtnListener,
                OnClickListener itemClickListener, int iconId)
        {
            Dialog dialog = null;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            
            if (iconId != NO_ICON)
            {
                //设置对话框图标
                builder.setIcon(iconId);
            }
            //设置对话框标题
            builder.setTitle(title);
            //设置列表选项
            builder.setItems(itemsString, itemClickListener);
            //设置确定按钮
            builder.setNegativeButton(negativeBtnName, negativeBtnListener);
            //创建一个消息对话框
            dialog = builder.create();
            
            return dialog;
        }
        
        
        /**
         * 创建自定义(含确认、取消)对话框
         * 
         * @param context 上下文 必填
         * @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填
         * @param title 标题 必填
         * @param positiveBtnName 确定按钮名称 必填
         * @param negativeBtnName 取消按钮名称 必填
         * @param positiveBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
         * @param view 对话框中自定义视图 必填
         * @return
         */
        public static Dialog createRandomDialog(Context context, String title, String positiveBtnName,
                String negativeBtnName, OnClickListener positiveBtnListener, 
                OnClickListener negativeBtnListener,View view, int iconId)
        {
            Dialog dialog = null;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            
            if (iconId != NO_ICON)
            {
                //设置对话框图标
                builder.setIcon(iconId);
            }
            //设置对话框标题
            builder.setTitle(title);
            builder.setView(view);
            //设置确定按钮
            builder.setPositiveButton(positiveBtnName, positiveBtnListener);
            //设置确定按钮
            builder.setNegativeButton(negativeBtnName, negativeBtnListener);
            //创建一个消息对话框
            dialog = builder.create();
            
            return dialog;
        }
        
    }

    使用示例:

    package com.example.encapsulation;
    
    import java.util.ArrayList;
    
    import dialog.DialogTool;
    import android.app.Activity;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.content.DialogInterface.OnMultiChoiceClickListener;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
    import application.CcsApplication;
    
    public class MainActivity extends Activity
    {
    
        Dialog dialog = null;
        String[] contents = {"第一项", "第二项", "第三项", "第四项", "第五项"};
        
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setDialog();
            
            CcsApplication ccsApplication = (CcsApplication)getApplicationContext();
            Log.v("serverIp", ccsApplication.getServerIp());
        }
    
        
        public void setDialog()
        {
            
            dialog = DialogTool.createMessageDialog(MainActivity.this, "标题", "内容",
                    "按钮", new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, DialogTool.NO_ICON);
            dialog.show();        
    
            
    /*        
            dialog = DialogTool.createConfirmDialog(MainActivity.this, "标题", "内容", "确定按钮", "取消按钮",
                    new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, DialogTool.NO_ICON);
            dialog.show();
    */        
            
    /*        
            dialog = DialogTool.createSingleChoiceDialog(MainActivity.this, "标题", contents, "确定按钮", "取消按钮",
                    new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, DialogTool.NO_ICON);
            dialog.show();
    */
            
    /*        
            dialog = DialogTool.createMultiChoiceDialog(MainActivity.this, "标题", contents, "确定按钮", "取消按钮",
                    new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, new OnMultiChoiceClickListener()
                    {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which, boolean isChecked)
                        {
                            // TODO Auto-generated method stub
                            
                        }
                    }, DialogTool.NO_ICON);
            dialog.show();
    */
            
    /*        
            dialog = DialogTool.createListDialog(MainActivity.this, "标题", contents, "取消按钮",
                    new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, DialogTool.NO_ICON);
            dialog.show();
    */        
            
    /*        
            EditText editText = new EditText(MainActivity.this);
            ImageView imageView = new ImageView(MainActivity.this);
            imageView.setImageResource(R.drawable.ic_launcher);
    //        View view = new View(MainActivity.this);
    //        ArrayList<View> childViews = new ArrayList<View>();
    //        childViews.add(imageView);
    //        childViews.add(editText);
    //        view.addChildrenForAccessibility(childViews);
            
            dialog = DialogTool.createRandomDialog(MainActivity.this, "标题", "确定按钮", "取消按钮",
                    new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, new OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
    
                        }
                    }, imageView, DialogTool.NO_ICON);
            dialog.show();
    */
        }
    }

    THE END


  • 相关阅读:
    Jdbc、Mybatis、Hibernate各自优缺点及区别
    java中的线程池原理
    JVN的理解
    "=="和 equals 方法究竟有什么区别?
    nginx基础之【study one】
    C#,WPF中使用多文本显示数据,并对其数据进行关键字高亮等操作
    C#、WPF中如何自定义鼠标样式
    SSM(Spring + Springmvc + Mybatis)框架面试题
    java基础面试题
    C#关于TreeView树在节点数较多时总是会出现闪烁的问题方法记录
  • 原文地址:https://www.cnblogs.com/zjjne/p/3350382.html
Copyright © 2020-2023  润新知