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


Android 常用对话框Dialog封装的更多相关文章

  1. (转载)Android常用的Dialog对话框用法

    Android常用的Dialog对话框用法 Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的 ...

  2. android常用对话框封装

    在android开发中,经常会用到对话框跟用户进行交互,方便用户可操作性:接下来就对常用对话框进行简单封装,避免在项目中出现冗余代码,加重后期项目的维护量:代码如有问题欢迎大家拍砖指正一起进步. 先贴 ...

  3. Android自定义对话框(Dialog)位置,大小

    代码: package angel.devil; import android.app.Activity;import android.app.Dialog;import android.os.Bun ...

  4. Android 自定义对话框(Dialog)位置,大小

    代码: package angel.devil; import android.app.Activity; import android.app.Dialog; import android.os.B ...

  5. Android常用的Dialog对话框用法

    Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的是V7 包里的AlertDialog. im ...

  6. android 开发 对话框Dialog详解

    转载请注明出处:红亮的专栏:http://blog.csdn.net/liang5630/article/details/44098899 Android中的对话框形式大致可分为五种:分别是一般对话框 ...

  7. Android之对话框Dialog

    首先是确认对话框 //确认对话框 private void showLog1() { AlertDialog.Builder dialog = new AlertDialog.Builder(this ...

  8. Android 开发 对话框Dialog dismiss和hide方法的区别

    http://ningtukun.blog.163.com/blog/static/186541445201310151539697/ dismiss和hide方法都可以隐藏对话框,在需要的时候也可以 ...

  9. Android常用工具类封装---SharedPreferencesUtil

    SharedPreferences常用于保存一些简单的数据,如记录用户操作的配置等,使用简单. public class SharedPreferencesUtil {              // ...

随机推荐

  1. cppunit官方文档浅析

    使用doxygen生成官方文档 cppunit使用了doxygen作为它的文档建设工具,所以我们要找的“官方文档”,其实就在cppunit的代码里面. 请先参考博文<下载doxygen>( ...

  2. HDU 4391 - Paint The Wall - 分块哈希入门

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4391 题意 : 给一段区间, 有两种操作 1 : 给 x 到 y 的区间染色为 z 2 : 查询 ...

  3. 红领的短板:线下“O”瓶颈_财经频道_一财网

    红领的短板:线下"O"瓶颈_财经频道_一财网 红领的短板:线下"O"瓶颈

  4. iOS开发之自定义输入框(利用UITextField及UITextView)

    drawRect的工作原理:首先苹果是不推荐我们直接使用drawRect进行工作的,直接调用他也是没有任何效果的.苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用 ...

  5. Iterator荟萃

    package com.starain.Iterator;/*代码整理快捷键为Ctrl+Shift+F * main方法输入快捷键main字符+Alt+/ * 输出快捷键sysout字符+Alt+/* ...

  6. 如何唯一确定一台iOS设备

    如果你的iOS应用需要针对设备做特定的操作,或者需要硬件的信息来进行判定等等的,你就需要对iOS设备进行唯一性的判定. 苹果设备有个先天的东西符合这个需求,UDID,这个东东用iTunes就可以看到, ...

  7. Managing Hierarchical Data in MySQL

    Managing Hierarchical Data in MySQL Introduction Most users at one time or another have dealt with h ...

  8. Codeforces 362D Fools and Foolproof Roads 构造题

    题目链接:点击打开链接 题意: 给定n个点 m条边的无向图 须要在图里添加p条边 使得图最后连通分量数为q 问是否可行,不可行输出NO 可行输出YES,并输出加入的p条边. set走起.. #incl ...

  9. 关于DOS下启动MySQL时提示服务名无效

    主要原因:启动时:net start mysql 而打开服务后发现,本地服务中mysql这个服务实际名字为mysql55,故启动语句应为:net  start mysql55: 以下摘自课程提问: 你 ...

  10. linux删除或隐藏命令历史记录history

    1.环境变量添加HISTCONTROL = ignorespace 在命令前面插入空格,这条命令会被 shell 忽略,也就意味着它不会出现在历史记录中.但是这种方法有个前提,只有在你的环境变量 HI ...