自定义android Dialog
1.自定义Dialog:
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.Window; public class HintDialog { Dialog mDialog = null;
private Context mContext;
private IHintDialog mDialogInstance = null; /**
* 构造函数
* @param context
*/
public HintDialog(Context context) {
this.mContext =context;
mDialog = new AlertDialog(mContext){
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && mDialogInstance !=null){
mDialogInstance.onKeyDown(keyCode, event);
return true;
}
return super.onKeyDown(keyCode, event);
};
};
mDialog.setCancelable(false);
mDialog.setCanceledOnTouchOutside(false);
} /**
* 构造函数
* @param iLayoutResId 此Dialog采用的自定义布局文件
* @param interfaceInstance 此Dialog需要实现的一些接口回调事件
*/
public void showDialog(int iLayoutResId,IHintDialog interfaceInstance){
if(mDialog == null || iLayoutResId == 0){
return;
}
mDialogInstance = interfaceInstance;
mDialog.show();
mDialog.setContentView(iLayoutResId);
Window window = mDialog.getWindow();
if(mDialogInstance!=null){
mDialogInstance.showWindowDetail(window);
}
} /**
* 销毁Dialog
*/
public void dissmissDialog(){
if(mDialog!=null && mDialog.isShowing()){
mDialog.dismiss();
}
} /**
* 判断Dialog是否显示
* @return
*/
public boolean isShowing(){
if(mDialog!=null && mDialog.isShowing()){
return mDialog.isShowing();
}
return false;
} /**
* 事件回调接口
*
*/
public interface IHintDialog{
public void onKeyDown(int keyCode,KeyEvent event);
public void showWindowDetail(Window window);
}
}
2.采用系统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 Jack_Lu
*
*/
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;
} }
自定义android Dialog的更多相关文章
- android中自定义的dialog中的EditText无法弹出输入法解决方案
1.解决无法弹出输入法: 在show()方法调用之前,用dialog.setView(new EditText(context))添加一个空的EditText,由于是自定义的AlertDialog,有 ...
- android dialog 模拟新浪、腾讯title弹框效果
http://blog.csdn.net/jj120522/article/details/7764183 首先我们看一下新浪微博的效果(其它就是一个dialog): 点 ...
- 自定义的dialog
自定义的dialog 其中包含置顶 删除 和取消 下面的是BaseDialog package com.free.csdn.view.dialog; import android.app.Dialo ...
- Android Dialog使用举例
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- CustomDialog——一个多功能、通用、可自定义的Dialog
CustomDialog--一个多功能.通用.可自定义的Dialog 依赖 compile 'com.github.SiberiaDante:CustomDialog:v1.0.1' 说明[Cus ...
- 自定义loading dialog --- 后背景透明
自定义loading dialog --- 后背景透明 <style name="loading_dialog" parent="android:style/ ...
- Android控件——7种形式的Android Dialog使用举例(转载)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- Android Dialog对话框的七种形式的使用
参考资料:http://www.oschina.net/question/54100_32486 注:代码进行了整理 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询 ...
- 8种形式的Android Dialog使用举例
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
随机推荐
- C#类、静态类、静态变量,初始化执行顺序
执行顺序: 类的静态变量 ↓ 类的静态构造函数 ↓ 类的普通变量 ↓ 基类的静态变量 ↓ 基类的静态构造函数 ↓ 基类的普通变量 ↓ 基类的构造函数 ↓ 类的构造函数
- CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...
- Excel Xll开发资料
下载地址:http://bbs.jjxj.org/forum.php?mod=viewthread&tid=37943&extra=page%3D1&page=1& 财 ...
- Unity3D之游戏暂停制作方法记录
在游戏开发中我们一般都需要涉及到一个功能:游戏暂停,但是这里指的暂停仅仅是核心模块的暂停,并不是整个游戏都暂停,比如一些UI和UI上的动画与特效是不能被暂停的,整个游戏都暂停了玩家该如何继续游戏呢. ...
- 调用DEDE日期时间格式整理大全
dedecms 日期时间格式大全,大家可以根据需要选择.DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strf ...
- C# 添加类库依赖
- 【项目实例】android开发游戏音效代码实例
//音效的音量 int streamVolume; //定义SoundPool 对象 private SoundPool soundPool; //定义HASH表 private HashMap< ...
- android广播集合,intent,action
android.permission.ACCESS_CHECKIN_PROPERTIES 同意读写訪问"properties"表在checkin数据库中,改值能够改动上传( All ...
- iOS开发——适配篇&App适配简单概括
App适配简单概括 1:适配:适应.兼容各种不同的情况 系统适配 针对不同版本的操作系统进行适配 屏幕适配 针对不同大小的屏幕尺寸进行适配 在用户眼中 屏幕是由无数个像素组成的 像素越多,屏幕越清晰 ...
- vim中设置Python自动补全
转自:http://blog.csdn.net/wangzhuo_0717/article/details/6942428 在VIM里面增加python的autocomplete功能的做法如下: 1. ...