很多时候,我们需要自己去定义dialog,目前我们就遇见了这样一个需求,我的想法是自己定义一个dialog,如果有list的话就使用listview,如果有msg的话就使用msg,并且取消和确定按钮也可自己定义。自定义一个dialog,好处是我们自己定义背景,自己定义事件,自己定义按钮,能很完美的达到自己想要的效果。

  1. public class CustomDialog extends Dialog {
  2. //实现默认构造函数
  3. public CustomDialog(Context context, int theme) {
  4. super(context, theme);
  5. // TODO Auto-generated constructor stub
  6. }
  7. protected CustomDialog(Context context, boolean cancelable,
  8. OnCancelListener cancelListener) {
  9. super(context, cancelable, cancelListener);
  10. // TODO Auto-generated constructor stub
  11. }
  12. public CustomDialog(Context context) {
  13. super(context);
  14. // TODO Auto-generated constructor stub
  15. }
  16. //所有的方法执行完都会返回一个Builder使得后面可以直接create和show
  17. public static class Builder {
  18. private Context context;
  19. private String title;
  20. private String message;
  21. private String positiveButtonText;//确定按钮
  22. private String negativeButtonText;//取消按钮
  23. private View contentView;
  24. private BaseAdapter adapter;//listview的adapter
  25. //确定按钮事件
  26. private DialogInterface.OnClickListener positiveButtonClickListener;
  27. //取消按钮事件
  28. private DialogInterface.OnClickListener negativeButtonClickListener;
  29. //listview的item点击事件
  30. private AdapterView.OnItemClickListener listViewOnclickListener;
  31. public Builder(Context context) {
  32. this.context = context;
  33. }
  34. //设置消息
  35. public Builder setMessage(String message) {
  36. this.message = message;
  37. return this;
  38. }
  39. /**
  40. *设置内容
  41. *
  42. * @param title
  43. * @return
  44. */
  45. public Builder setMessage(int message) {
  46. this.message = (String) context.getText(message);
  47. return this;
  48. }
  49. /**
  50. * 设置标题
  51. *
  52. * @param title
  53. * @return
  54. */
  55. public Builder setTitle(int title) {
  56. this.title = (String) context.getText(title);
  57. return this;
  58. }
  59. /**
  60. *设置标题
  61. *
  62. * @param title
  63. * @return
  64. */
  65. public Builder setTitle(String title) {
  66. this.title = title;
  67. return this;
  68. }
  69. //设置适配器
  70. public Builder setAdapter(BaseAdapter adapter) {
  71. this.adapter = adapter;
  72. return this;
  73. }
  74. //设置点击事件
  75. public Builder setOnClickListener(AdapterView.OnItemClickListener listViewOnclickListener) {
  76. this.listViewOnclickListener = listViewOnclickListener;
  77. return this;
  78. }
  79. //设置整个背景
  80. public Builder setContentView(View v) {
  81. this.contentView = v;
  82. return this;
  83. }
  84. /**
  85. * 设置确定按钮和其点击事件
  86. *
  87. * @param positiveButtonText
  88. * @return
  89. */
  90. public Builder setPositiveButton(int positiveButtonText,
  91. DialogInterface.OnClickListener listener) {
  92. this.positiveButtonText = (String) context
  93. .getText(positiveButtonText);
  94. this.positiveButtonClickListener = listener;
  95. return this;
  96. }
  97. public Builder setPositiveButton(String positiveButtonText,
  98. DialogInterface.OnClickListener listener) {
  99. this.positiveButtonText = positiveButtonText;
  100. this.positiveButtonClickListener = listener;
  101. return this;
  102. }
  103. //设置取消按钮和其事件
  104. public Builder setNegativeButton(int negativeButtonText,
  105. DialogInterface.OnClickListener listener) {
  106. this.negativeButtonText = (String) context
  107. .getText(negativeButtonText);
  108. this.negativeButtonClickListener = listener;
  109. return this;
  110. }
  111. public Builder setNegativeButton(String negativeButtonText,
  112. DialogInterface.OnClickListener listener) {
  113. this.negativeButtonText = negativeButtonText;
  114. this.negativeButtonClickListener = listener;
  115. return this;
  116. }
  117. //createview方法
  118. public CustomDialog create() {
  119. LayoutInflater inflater = (LayoutInflater) context
  120. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  121. // 设置其风格
  122. final CustomDialog dialog = new CustomDialog(context,R.style.CustomProgressDialog);
  123. View layout = inflater.inflate(R.layout.dialog_custom_layout, null);
  124. dialog.addContentView(layout, new LayoutParams(
  125. LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  126. // 设置标题
  127. ((TextView) layout.findViewById(R.id.title)).setText(title);
  128. //设置listview的adapter如果没有就隐藏listview
  129. if(adapter != null && adapter.getCount()>0){
  130. ListView listView = (ListView)layout.findViewById(R.id.listView);
  131. listView.setAdapter(adapter);
  132. if(listViewOnclickListener!=null){
  133. listView.setOnItemClickListener(listViewOnclickListener);
  134. }
  135. }else{
  136. layout.findViewById(R.id.listView).setVisibility(
  137. View.GONE);
  138. }
  139. //设置确定按钮
  140. if (positiveButtonText != null) {
  141. ((Button) layout.findViewById(R.id.positiveButton))
  142. .setText(positiveButtonText);
  143. if (positiveButtonClickListener != null) {
  144. ((Button) layout.findViewById(R.id.positiveButton))
  145. .setOnClickListener(new View.OnClickListener() {
  146. public void onClick(View v) {
  147. positiveButtonClickListener.onClick(dialog,
  148. DialogInterface.BUTTON_POSITIVE);
  149. }
  150. });
  151. }
  152. } else {
  153. // 如果没有确定按钮就将其隐藏
  154. layout.findViewById(R.id.positiveButton).setVisibility(
  155. View.GONE);
  156. }
  157. // 设置取消按钮
  158. if (negativeButtonText != null) {
  159. ((Button) layout.findViewById(R.id.negativeButton))
  160. .setText(negativeButtonText);
  161. if (negativeButtonClickListener != null) {
  162. ((Button) layout.findViewById(R.id.negativeButton))
  163. .setOnClickListener(new View.OnClickListener() {
  164. public void onClick(View v) {
  165. negativeButtonClickListener.onClick(dialog,
  166. DialogInterface.BUTTON_NEGATIVE);
  167. }
  168. });
  169. }
  170. } else {
  171. // 如果没有取消按钮就将其隐藏
  172. layout.findViewById(R.id.negativeButton).setVisibility(
  173. View.GONE);
  174. }
  175. // 设置内容
  176. if (message != null) {
  177. ((TextView) layout.findViewById(R.id.message)).setText(message);
  178. } else if (contentView != null) {
  179. // if no message set
  180. // 添加view
  181. ((LinearLayout) layout.findViewById(R.id.message))
  182. .removeAllViews();
  183. ((LinearLayout) layout.findViewById(R.id.message)).addView(
  184. contentView, new LayoutParams(
  185. LayoutParams.WRAP_CONTENT,
  186. LayoutParams.WRAP_CONTENT));
  187. }
  188. dialog.setContentView(layout);
  189. return dialog;
  190. }
  191. }
  192. }

Android自定义dialogdemo的更多相关文章

  1. android 自定义动画

    android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...

  2. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

  3. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  4. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  5. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  6. Android 自定义View (五)——实践

    前言: 前面已经介绍了<Android 自定义 view(四)-- onMeasure 方法理解>,那么这次我们就来小实践下吧 任务: 公司现有两个任务需要我完成 (1)监测液化天然气液压 ...

  7. Android 自定义 view(四)—— onMeasure 方法理解

    前言: 前面我们已经学过<Android 自定义 view(三)-- onDraw 方法理解>,那么接下我们还需要继续去理解自定义view里面的onMeasure 方法 推荐文章: htt ...

  8. Android 自定义 view(三)—— onDraw 方法理解

    前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自 ...

  9. Android 自定义view(二) —— attr 使用

    前言: attr 在前一篇文章<Android 自定义view -- attr理解>已经简单的进行了介绍和创建,那么这篇文章就来一步步说说attr的简单使用吧 自定义view简单实现步骤 ...

随机推荐

  1. AngularJs-指令和指令之间的交互(动感超人)

    前言: 上节我们学习到了指令和控制器之间的交互,通过给指令添加动作,调用了控制器中的方法.本节我们学习指令和指令之间是如何交互的,我们通过一个小游戏来和大家一起学习,听大漠老师说这是国外的人写的dem ...

  2. Java Web整合开发实战:基于Struts 2+Hibernate+Spring 目录

    第1篇 Java Web开发基础第1章 Web的工作机制( 教学视频:31分钟) 1.1 理解Web的概念 1.1.1 Web的定义 1.1.2 Web的三个核心标准 1.2 C/S与B/S两种软件体 ...

  3. jstl标签用法

     bean的uri的路径 bean标签是属于struts中的标签,使用要在 Struts 1.3 Libraries中 struts-taglib-1.3.8.jar 中META-INFtld ...

  4. DELL R710服务器做RAID5磁盘阵列图文教程

    本文转载于:http://www.jb51.net/article/53707.htm,只为做笔记使用 同时我们还可以选择一块硬盘做热备盘,就是说当配的raid5中有一个硬盘坏了的时候,它立马顶上,然 ...

  5. 从零开始设计SOA框架(二):请求/响应参数的设计

    每个接口都有请求参数.响应参数.其中请求参数分为公共参数和业务参数.响应参数分为两类:正常的响应参数.统一的错误参数   一.请求参数 1.公共参数:每个接口都有的参数,主要包含appkey.时间戳. ...

  6. PowerDesigner-如何导出建表sql脚本

    1 按照数据库类型,切换数据库. Database-> Change Current DBMS... 2 生成sql脚本 Database -> Database Generation 的 ...

  7. 图解Android - Android GUI 系统 (2) - 窗口管理 (View, Canvas, Window Manager)

    Android 的窗口管理系统 (View, Canvas, WindowManager) 在图解Android - Zygote 和 System Server 启动分析一 文里,我们已经知道And ...

  8. 模拟Modbus协议问题

    问题: 在嵌入式系统开发中,Modbus协议是工业控制系统中广泛应用的一种协议.本题用来简单模拟Modbus协议,只需根据条件生成符合该协议的数据帧,并解析所获取的数据.假设设备使用的协议发送数据格式 ...

  9. 【codevs1409】 拦截导弹 2

    http://codevs.cn/problem/1409/ (题目链接) 题意 给出n个三维的导弹,每次拦截只能打x,y,z严格上升的若干个导弹,求最多能一次拦截下多少个导弹,以及最少拦截几次将所有 ...

  10. Threat Risk Modeling Learning

    相关学习资料 http://msdn.microsoft.com/en-us/library/aa302419(d=printer).aspx http://msdn.microsoft.com/li ...