自定义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的过程中,我归纳了一 ...
随机推荐
- Codeforces 599C Day at the Beach(想法题,排序)
C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunate ...
- ajax乱码问题 服务端 客户端 两种的解决方案--转载
今天弄了一天的Ajax中文乱码问题,Ajax的乱码问题分为两种: 1. JavaScript输出的中文乱码, 比如:alert("中文乱码测试"); 2. 这第二种就是Ajax从服 ...
- C#学习笔记(十):反射
反射 放射是指在程序运行时动态的获取类的信息的机制,我们下面来看看C#中的反射. Type Type 为 System.Reflection 功能的根,也是访问元数据的主要方式. 使用 Type 的成 ...
- Elasticsearch和mysql数据同步(elasticsearch-jdbc)
1.介绍 对mysql.oracle等数据库数据进行同步到ES有三种做法:一个是通过elasticsearch提供的API进行增删改查,一个就是通过中间件进行数据全量.增量的数据同步,另一个是通过收集 ...
- Hibernate的配置
配置Hibernate 1.在项目中引入Hibernate的Jar包 在 WebContent/WEB-INF/lib 目录下 导入jar包 2.在Java Resources 下创建 hiberna ...
- java搭建finagle(1)
1.新建maven项目 2.pom文件添加依赖 添加3个主要依赖<dependency> <groupId>com.twitter</groupId> <ar ...
- 解决statusStrip控件上的项目不能靠右对齐的问题
在c#中用到了状态栏控件StatusStrip,但当我想把StatusStrip上某个StatusLabel靠右对齐时出了问题. 按照MSDN中的办法,是设置ToolStripStatusLabel的 ...
- delphi ExecWB
TWebBrowser.ExecWB 关键点 procedure ExecWB(cmdID: OLECMDID; cmdexecopt: OLECMDEXECOPT); overload; 实现过程 ...
- android147 360 程序锁
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- jquery.validate.js使用id验证控件
jquery.validate.js默认的是元素的name. 例如:<input name="username" id="username" size=& ...