Android自定义dialogdemo
很多时候,我们需要自己去定义dialog,目前我们就遇见了这样一个需求,我的想法是自己定义一个dialog,如果有list的话就使用listview,如果有msg的话就使用msg,并且取消和确定按钮也可自己定义。自定义一个dialog,好处是我们自己定义背景,自己定义事件,自己定义按钮,能很完美的达到自己想要的效果。
- public class CustomDialog extends Dialog {
- //实现默认构造函数
- public CustomDialog(Context context, int theme) {
- super(context, theme);
- // TODO Auto-generated constructor stub
- }
- protected CustomDialog(Context context, boolean cancelable,
- OnCancelListener cancelListener) {
- super(context, cancelable, cancelListener);
- // TODO Auto-generated constructor stub
- }
- public CustomDialog(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- //所有的方法执行完都会返回一个Builder使得后面可以直接create和show
- public static class Builder {
- private Context context;
- private String title;
- private String message;
- private String positiveButtonText;//确定按钮
- private String negativeButtonText;//取消按钮
- private View contentView;
- private BaseAdapter adapter;//listview的adapter
- //确定按钮事件
- private DialogInterface.OnClickListener positiveButtonClickListener;
- //取消按钮事件
- private DialogInterface.OnClickListener negativeButtonClickListener;
- //listview的item点击事件
- private AdapterView.OnItemClickListener listViewOnclickListener;
- public Builder(Context context) {
- this.context = context;
- }
- //设置消息
- public Builder setMessage(String message) {
- this.message = message;
- return this;
- }
- /**
- *设置内容
- *
- * @param title
- * @return
- */
- public Builder setMessage(int message) {
- this.message = (String) context.getText(message);
- return this;
- }
- /**
- * 设置标题
- *
- * @param title
- * @return
- */
- public Builder setTitle(int title) {
- this.title = (String) context.getText(title);
- return this;
- }
- /**
- *设置标题
- *
- * @param title
- * @return
- */
- public Builder setTitle(String title) {
- this.title = title;
- return this;
- }
- //设置适配器
- public Builder setAdapter(BaseAdapter adapter) {
- this.adapter = adapter;
- return this;
- }
- //设置点击事件
- public Builder setOnClickListener(AdapterView.OnItemClickListener listViewOnclickListener) {
- this.listViewOnclickListener = listViewOnclickListener;
- return this;
- }
- //设置整个背景
- public Builder setContentView(View v) {
- this.contentView = v;
- return this;
- }
- /**
- * 设置确定按钮和其点击事件
- *
- * @param positiveButtonText
- * @return
- */
- public Builder setPositiveButton(int positiveButtonText,
- DialogInterface.OnClickListener listener) {
- this.positiveButtonText = (String) context
- .getText(positiveButtonText);
- this.positiveButtonClickListener = listener;
- return this;
- }
- public Builder setPositiveButton(String positiveButtonText,
- DialogInterface.OnClickListener listener) {
- this.positiveButtonText = positiveButtonText;
- this.positiveButtonClickListener = listener;
- return this;
- }
- //设置取消按钮和其事件
- public Builder setNegativeButton(int negativeButtonText,
- DialogInterface.OnClickListener listener) {
- this.negativeButtonText = (String) context
- .getText(negativeButtonText);
- this.negativeButtonClickListener = listener;
- return this;
- }
- public Builder setNegativeButton(String negativeButtonText,
- DialogInterface.OnClickListener listener) {
- this.negativeButtonText = negativeButtonText;
- this.negativeButtonClickListener = listener;
- return this;
- }
- //createview方法
- public CustomDialog create() {
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- // 设置其风格
- final CustomDialog dialog = new CustomDialog(context,R.style.CustomProgressDialog);
- View layout = inflater.inflate(R.layout.dialog_custom_layout, null);
- dialog.addContentView(layout, new LayoutParams(
- LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
- // 设置标题
- ((TextView) layout.findViewById(R.id.title)).setText(title);
- //设置listview的adapter如果没有就隐藏listview
- if(adapter != null && adapter.getCount()>0){
- ListView listView = (ListView)layout.findViewById(R.id.listView);
- listView.setAdapter(adapter);
- if(listViewOnclickListener!=null){
- listView.setOnItemClickListener(listViewOnclickListener);
- }
- }else{
- layout.findViewById(R.id.listView).setVisibility(
- View.GONE);
- }
- //设置确定按钮
- if (positiveButtonText != null) {
- ((Button) layout.findViewById(R.id.positiveButton))
- .setText(positiveButtonText);
- if (positiveButtonClickListener != null) {
- ((Button) layout.findViewById(R.id.positiveButton))
- .setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- positiveButtonClickListener.onClick(dialog,
- DialogInterface.BUTTON_POSITIVE);
- }
- });
- }
- } else {
- // 如果没有确定按钮就将其隐藏
- layout.findViewById(R.id.positiveButton).setVisibility(
- View.GONE);
- }
- // 设置取消按钮
- if (negativeButtonText != null) {
- ((Button) layout.findViewById(R.id.negativeButton))
- .setText(negativeButtonText);
- if (negativeButtonClickListener != null) {
- ((Button) layout.findViewById(R.id.negativeButton))
- .setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- negativeButtonClickListener.onClick(dialog,
- DialogInterface.BUTTON_NEGATIVE);
- }
- });
- }
- } else {
- // 如果没有取消按钮就将其隐藏
- layout.findViewById(R.id.negativeButton).setVisibility(
- View.GONE);
- }
- // 设置内容
- if (message != null) {
- ((TextView) layout.findViewById(R.id.message)).setText(message);
- } else if (contentView != null) {
- // if no message set
- // 添加view
- ((LinearLayout) layout.findViewById(R.id.message))
- .removeAllViews();
- ((LinearLayout) layout.findViewById(R.id.message)).addView(
- contentView, new LayoutParams(
- LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT));
- }
- dialog.setContentView(layout);
- return dialog;
- }
- }
- }
Android自定义dialogdemo的更多相关文章
- android 自定义动画
android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...
- Android自定义View 画弧形,文字,并增加动画效果
一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类 B ...
- Android自定义View4——统计图View
1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...
- (转)[原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
- Android 自定义View (五)——实践
前言: 前面已经介绍了<Android 自定义 view(四)-- onMeasure 方法理解>,那么这次我们就来小实践下吧 任务: 公司现有两个任务需要我完成 (1)监测液化天然气液压 ...
- Android 自定义 view(四)—— onMeasure 方法理解
前言: 前面我们已经学过<Android 自定义 view(三)-- onDraw 方法理解>,那么接下我们还需要继续去理解自定义view里面的onMeasure 方法 推荐文章: htt ...
- Android 自定义 view(三)—— onDraw 方法理解
前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自 ...
- Android 自定义view(二) —— attr 使用
前言: attr 在前一篇文章<Android 自定义view -- attr理解>已经简单的进行了介绍和创建,那么这篇文章就来一步步说说attr的简单使用吧 自定义view简单实现步骤 ...
随机推荐
- 写在读ng之前的基础知识----笔记
如果要看angular的代码, 先把这个给看了, 司徒的干货. /******************************************************************* ...
- Linux CentOS6.5下编译安装MySQL 5.6.16【给力详细教程】
一.编译安装MySQL前的准备工作 安装编译源码所需的工具和库 yum install gcc gcc-c++ ncurses-devel perl 安装cmake,从http://www.cmake ...
- .net架构设计读书笔记--第三章 第10节 命令职责分离(CQRS)简介(Introducing CQRS)
一.分离查询命令 Separating commands from queries 早期的面向DDD设计方法的难点是如何设计一个类,这个类要包含域的方方面面.通常来说,任务软件系统方法调用可以 ...
- 教你一步一步实现一个Promise
Promise我想现在大家都非常熟悉了,主要作用就是解决异步回调问题,这里简单介绍下. Promise规范是CommonJS规范之一,而Promise规范又分了好多种,比如 Promises/A.Pr ...
- Ajax、反向Ajax和WebSocket 概念
Ajax 异步的JavaScript和XML(Asynchronous JavaScript and XML,Ajax),一种可通过JavaScript来访问的浏览器功能特性,其允许脚本向幕后的网站发 ...
- Oracle 11g 默认用户名和密码
安装ORACLE时,若没有为下列用户重设密码,则其默认密码如下: 用户名 / 密码 登录身份 说明 ...
- 【BZOJ-3545&3551】Peaks&加强版 Kruskal重构树 + 主席树 + DFS序 + 倍增
3545: [ONTAK2010]Peaks Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1202 Solved: 321[Submit][Sta ...
- 【bzoj1857】 Scoi2010—传送带
http://www.lydsy.com/JudgeOnline/problem.php?id=1857 (题目链接) 题意 给出两条线段AB和CD,在AB上的速度为P,在CD上的速度为Q,在AB,C ...
- JAVA多线程经典范列:生产者与消费者
* 第一:生产者 生产的消费品 存放到仓库中,当仓库满时,生产者停止生产 * 第二:消费者 到仓库中 使用消费品,当仓库没有消费品时,停止消费 * 第三:生产者 在仓库满停止生产后 通知消费者去消费 ...
- Java线程之间通信
用多线程的目的:更好的利用CPU的资源.因为所有的多线程代码都可以用单线程来实现. 多线程:指的是这个程序(一个进程)运行时产生了不止一个线程. 并行:多个CPU实例或者多台机器同时执行一段处理逻辑, ...