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
Android 常用对话框Dialog封装的更多相关文章
- (转载)Android常用的Dialog对话框用法
Android常用的Dialog对话框用法 Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的 ...
- android常用对话框封装
在android开发中,经常会用到对话框跟用户进行交互,方便用户可操作性:接下来就对常用对话框进行简单封装,避免在项目中出现冗余代码,加重后期项目的维护量:代码如有问题欢迎大家拍砖指正一起进步. 先贴 ...
- Android自定义对话框(Dialog)位置,大小
代码: package angel.devil; import android.app.Activity;import android.app.Dialog;import android.os.Bun ...
- Android 自定义对话框(Dialog)位置,大小
代码: package angel.devil; import android.app.Activity; import android.app.Dialog; import android.os.B ...
- Android常用的Dialog对话框用法
Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的是V7 包里的AlertDialog. im ...
- android 开发 对话框Dialog详解
转载请注明出处:红亮的专栏:http://blog.csdn.net/liang5630/article/details/44098899 Android中的对话框形式大致可分为五种:分别是一般对话框 ...
- Android之对话框Dialog
首先是确认对话框 //确认对话框 private void showLog1() { AlertDialog.Builder dialog = new AlertDialog.Builder(this ...
- Android 开发 对话框Dialog dismiss和hide方法的区别
http://ningtukun.blog.163.com/blog/static/186541445201310151539697/ dismiss和hide方法都可以隐藏对话框,在需要的时候也可以 ...
- Android常用工具类封装---SharedPreferencesUtil
SharedPreferences常用于保存一些简单的数据,如记录用户操作的配置等,使用简单. public class SharedPreferencesUtil { // ...
随机推荐
- 【转】silverlight 跨域访问
作者:MIDI 来源:博客园 发布时间:2010-01-01 17:39 阅读:204 次 原文链接 [收藏] 在 Silverlight 使用 WebService .WCF.We ...
- POJ-1200(哈希)
2015-08-19 题意:给出两个数n,nc,并给出一个由nc种字符组成的字符串.求这个字符串中长度为n的子串有多少种. 分析: 1.这个题不用匹配,因为不高效. 2.将长度为n的子串看作n位的nc ...
- 《算法实战策略》-chaper19-队列、栈和双端队列
对于计算机专业的学生来说,他们一定会很熟悉一句话:程序设计 = 算法 + 数据结构.而根据笔者的理解,所谓程序设计其实就是为了编程解决实际问题,所谓算法是一种解决问题某种思维的方法,但是思维需要得到编 ...
- Selenium 初见
Selenium名字的来源 在这里,我还想说一下关于Selenium名字的来源,很有意思的: >:Selenium的中文名为“硒”,是一种化学元素的名字,它对汞 (Mercury)有天然的解毒作 ...
- java笔记8之选择结构IF
注意1 A比较表达式无论简单还是复杂,结果必须是boolean类型 B:if语句控制的语句体如果是一条语句,大括号可以省略: 如果是多条语句,就不能省略.建议永远不要省 ...
- 使用Intent实现Activity的隐式跳转
相比于显式Intent,隐式Intent 则含蓄了许多,它并不明确指出我们想要启动哪一个活动,而是指定了一系列更为抽象的action 和category 等信息,然后交由系统去分析这个Intent,并 ...
- 2015 UESTC Winter Training #7【2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest】
2015 UESTC Winter Training #7 2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest 据 ...
- TextView过长显示省略号, TextView文字中间加横线
1.TextView显示的内容过长时自动显示省略号: 省略号的位置:android:ellipsize="end" 省略号在结尾android:ellipsize=" ...
- 使用DOM进行xml文档的crud(增删改查)操作<操作详解>
很多朋友对DOM有感冒,这里我花了一些时间写了一个小小的教程,这个能看懂,会操作了,我相信基于DOM的其它API(如JDOM,DOM4J等)一般不会有什么问题. 后附java代码,也可以下载(可点击这 ...
- PHP String
PHP 5 String 函数 PHP String 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. 函数 描述 addcslashes() 返回在指定的字符前添加反斜杠的字符串. add ...