自定义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的过程中,我归纳了一 ...
随机推荐
- ACM OJ Collection
浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 同济大学(TJU):http://acm.t ...
- VMware搭建12.0搭建Mac OS10.11详细过程
1.软件准备 1.1VMware12.0 1.2VMware增强包 1.3Mac OS10.11 cdr(相当于dmg) 1.4securable.exe 2.软件破解 2.1VMware输入序列号破 ...
- 织梦dedecms后台添加图片style全部都变成st<x>yle的解决办法
可乐站长在建站的时候,上传缩略图喜欢输入图片路径,不喜欢上传图片,有几次我上传图片路径为:/style/image/**.jpg,然后返回修改后,图片为路径却为:/st<x>yle/ima ...
- 栈的应用1——超级计算器(中缀与后缀表达式)C语言
这里要学的程序主要用来实现一个功能——输入表达式输出结果,也就是一个计算器.效果如下: 这个程序主要有两个步骤:1.把中缀表达式转换为后缀表达式:2.计算后缀表达式的结果. 首先先明白几个问题: 1. ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- Chrome Apps将可以打包成iOS或Android应用
Chrome Apps 将可以在 iOS 和 Android 设备上独立运行了.开发者只要使用 Google今天 提供的工具集(toolchain)将自己的 Web App 打包,并将生成的应用上传到 ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- iOS开发——总结篇&关键字介绍
关键字介绍 在做iOS开发中,系统的各个关键字处处都是,但是未必每一个关键字都是那么的明白,所以罗列出一些关键字供大家互相学习,有不完善之处请指正,谢谢 atomic atomic是Objc使用的一种 ...
- Metadata Lock原理2
同事说开发机更改一个表结构,加字段,但是一直挂在那里,没反应.一开始以为表测试数据量很大,因为mysql增加表字段会重写表,后来看了下数据量很小,就另外查看过程.原因分析和处理如下: 一.环境 m ...
- xtrabackup原理2
XTRABACKUP备份原理实现细节——对淘宝数据库内核月报补充 前言 淘宝3月的数据库内核月报对xtrabackup的备份原理做了深入的分析,写的还是很不错.不过Inside君在看完之后,感觉没有对 ...