自定义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的过程中,我归纳了一 ...
随机推荐
- STC89C52RC片内资源介绍
STC89C52RC片内有:用户应用程序区(AP)8K,地址0000h-1FFFh. 数据flash区(EEPROM)4K,2000h-2FFFh ISP引导区空间1K/2k/4k. RAM 512B ...
- ASP.NET项目中引用全局dll
在ASP.NET项目中,有些dll是全局dll,也就是说,没有放在单个项目的引用中.它们一般存放在如下目录C:\Windows\assembly中 这个时候,我们需要在单个项目中引用他们,应该如何做呢 ...
- iOS应用内HTTP服务上传文件
相信很多朋友都用过AirAV.100tv这类iOS视频播放应用中通过Wifi,从PC上输入Web地址上传文件到iOS设备上,我也一直想实现这个功能,苦于知识掌握有限,后来在其他群友的指导下参照很多大神 ...
- HQL和Criteria
HQL: public boolean doCreate(Dept vo) throws Exception { return this.sessionFactory.getCurrentSessio ...
- VHDL的testbench的编写(转)
大多数硬件设计人员对verilog的testbench比较熟悉,那是因为verilog被设计出来的目的就是为了用于测试使用,也正是因为这样verilog的语法规则才被设计得更像C语言,而verilog ...
- hdu3001 Travelling
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- [置顶] VC++界面编程之--自定义CEdit(编辑框)皮肤
自定义编辑框是登陆界面经常用到的效果,所以我也模仿站酷网的素材做了个. 要想继承CCustomDraw来完全自绘CEdit控件,是不太可行的方案,因为一旦你完全重绘,那么你需要额外做以下几件事: 1. ...
- [Angular 2] Import custom module
The application structure: in app.module.ts: import { NgModule} from "@angular/core"; impo ...
- [Javascript] Other functor
EventStream: You can use RxJS, BaconJS or any reactive programming lib you want: var id_s = map(func ...
- android152 笔记 4
42. Android中Task任务栈的分配. 首先我们来看下Task的定义,Google是这样定义Task的:a task is what the user experiences as an &q ...