自定义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的过程中,我归纳了一 ...
随机推荐
- MongoDB 字段增长
MongoDB中存储的文档必须有一个"_id"键.这个键的值可以是任何类型的,默认是个ObjectId对象. ObjectId 是一个12字节 BSON 类型数据,有以下格式: 前 ...
- #JAVA操作LDAP
package com.wisdombud.unicom.monitor.ldap; import java.util.ArrayList; import org.slf4j.Logger; impo ...
- MongoDB的update有关问题(JAVA)——如何一次更新所有的相同记录
MongoDB的update问题(JAVA)——怎么一次更新所有的相同记录用如下这个函数:public WriteResult update(DBObject q, DBObject o, boo ...
- UVa 1252 Twenty Questions (状压DP+记忆化搜索)
题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...
- sql 调用函数的方法
USE [ChangHong_612]GO/****** Object: StoredProcedure [dbo].[st_MES_RptInspectWeight] Script Date: 09 ...
- SQL Server 数据类型映射 (ADO.NET)
SQL Server 数据类型映射 (ADO.NET) .NET Framework 3.5 更新:November 2007 SQL Server 和 .NET Framework 基于不同的类型系 ...
- TinyTask Portable(录制和回放电脑操作过程)
一直想找个软件,可以回放自己或他人编码的过程.我认为,只有亲眼看到别人是怎么做的,才能更好的了解他人的思路. 虽然找到http://thecodeplayer.com/这个,但它只能回放它上面的代码, ...
- 更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found
在Xcode中当你在更新了你得证书而再重新编译你的程序,真机调试一直会出现 Code Sign error: Provisioning profile ‘XXXX’ can't be found是不是 ...
- Mysql中关于 group_concat函数详解
group_concat()主要功能:能将相同的行组合起来 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Sepa ...
- [小技巧]設定Reporting Services 2008 發生報表管理員權限不足
转载 http://www.dotblogs.com.tw/dorlis.tsao/archive/2011/01/17/20860.aspx 在自己Windows 7 professional的電腦 ...