46.Android 自己定义Dialog
46.Android 自己定义Dialog
前言
提供两套自己定义Dialog模板
第一种。提示Dialog,有消失时间。
另外一种,菜单Dialog,用于用户交互。
提示Dialog
CustomDialog
public class CustomDialog extends Dialog {
private TextView dialogTV;
private static final long DEFAULT_DURATION = 1000L;
private static final String DEFAULT_CONTENT = "";
private long duration;
private String content;
private DialogCallback callback;
public CustomDialog(Context context) {
super(context, R.style.custom_dialog);
this.initViews(context);
}
/**
* Creates a dialog window that uses a custom dialog style.
* <p/>
* The supplied {@code context} is used to obtain the window manager and
* base theme used to present the dialog.
* <p/>
* The supplied {@code theme} is applied on top of the context's theme. See
* <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">
* Style and Theme Resources</a> for more information about defining and
* using styles.
*
* @param context the context in which the dialog should run
* @param themeResId a style resource describing the theme to use for the
* window, or {@code 0} to use the default dialog theme
*/
public CustomDialog(Context context, int themeResId) {
super(context, themeResId);
this.initViews(context);
}
public CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
this.initViews(context);
}
private void initViews(Context context) {
this.setContentView(R.layout.dialog_custom);
this.dialogTV = (TextView) this.findViewById(R.id.custom_dialog_tv);
}
@Override
public void show() {
super.show();
this.dialogTV.setText(TextUtils.isEmpty(this.content) ? DEFAULT_CONTENT : this.content);
long showDuration = this.duration > 0L ? this.duration : DEFAULT_DURATION;
new Handler().postDelayed(new Runnable() {
public void run() {
if (CustomDialog.this.isShowing()) {
CustomDialog.this.dismiss();
if (CustomDialog.this.callback != null) CustomDialog.this.callback.onDismiss();
}
}
}, showDuration);
}
public void setTextDrawable(Drawable drawable) {
if (drawable == null) return;
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
this.dialogTV.setCompoundDrawables(drawable, null, null, null);
}
public interface DialogCallback {
void onDismiss();
}
public static class DialogBuilder {
private static String contextHashCode;
private static CustomDialog dialog;
public static DialogBuilder ourInstance;
public static DialogBuilder getInstance(Context context) {
if (ourInstance == null) ourInstance = new DialogBuilder();
String hashCode = String.valueOf(context.hashCode());
/**
* 不同一个Activity
*/
if (!hashCode.equals(String.valueOf(contextHashCode))) {
contextHashCode = hashCode;
dialog = new CustomDialog(context);
}
return ourInstance;
}
public DialogBuilder setDuration(long duration) {
dialog.duration = duration;
return this;
}
public DialogBuilder setContent(String content) {
dialog.content = content;
return this;
}
public DialogBuilder setDrawable(Drawable drawable) {
dialog.setTextDrawable(drawable);
return this;
}
public DialogBuilder setCallback(DialogCallback callback) {
dialog.callback = callback;
return this;
}
public DialogBuilder setCanceledOnTouchOutside(boolean cancel) {
dialog.setCanceledOnTouchOutside(cancel);
return this;
}
public CustomDialog getDialog() {
return dialog;
}
}
}
dialog_custom.xml
<?
xml version="1.0" encoding="utf-8"?
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/custom_dialog_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_dialog_custom_tv"
android:drawableLeft="@mipmap/dialog_custom_tv_drawable"
android:drawablePadding="5dp"
android:drawableStart="@mipmap/dialog_custom_tv_drawable"
android:gravity="center"
android:text="成功"
android:textColor="#ff666666"
android:textSize="15sp" />
</LinearLayout>
R.style.custom_dialog
<style name="custom_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
</style>
提示Dialog 效果图
菜单Dialog
MenuDialog
public class MenuDialog extends Dialog {
private TextView caseTV;
private TextView helpTV;
public MenuDialog(Context context) {
super(context, R.style.menu_dialog);
this.initViews(context);
}
/**
* Creates a dialog window that uses a custom dialog style.
* <p/>
* The supplied {@code context} is used to obtain the window manager and
* base theme used to present the dialog.
* <p/>
* The supplied {@code theme} is applied on top of the context's theme. See
* <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">
* Style and Theme Resources</a> for more information about defining and
* using styles.
*
* @param context the context in which the dialog should run
* @param themeResId a style resource describing the theme to use for the
* window, or {@code 0} to use the default dialog theme
*/
public MenuDialog(Context context, int themeResId) {
super(context, themeResId);
this.initViews(context);
}
public MenuDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
this.initViews(context);
}
private void initViews(Context context) {
this.setContentView(R.layout.dialog_menu);
this.caseTV = (TextView) this.findViewById(R.id.dialog_menu_case_tv);
this.helpTV = (TextView) this.findViewById(R.id.dialog_menu_help_tv);
}
public void setTextDrawable(Drawable drawable) {
if (drawable == null) return;
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
}
public static class DialogBuilder {
private static String contextHashCode;
private static MenuDialog dialog;
public static DialogBuilder ourInstance;
public static DialogBuilder getInstance(Context context) {
if (ourInstance == null) ourInstance = new DialogBuilder();
String hashCode = String.valueOf(context.hashCode());
/**
* 不同一个Activity
*/
if (!hashCode.equals(String.valueOf(contextHashCode))) {
contextHashCode = hashCode;
dialog = new MenuDialog(context);
}
return ourInstance;
}
public DialogBuilder setCaseListenser(View.OnClickListener listener) {
dialog.caseTV.setOnClickListener(listener);
return this;
}
public DialogBuilder setHelpListener(View.OnClickListener listener) {
dialog.helpTV.setOnClickListener(listener);
return this;
}
public DialogBuilder setCanceledOnTouchOutside(boolean cancel) {
dialog.setCanceledOnTouchOutside(cancel);
return this;
}
public MenuDialog getDialog() {
return dialog;
}
}
}
dialog_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/dialog_menu_case_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_dialog_menu_item"
android:drawablePadding="13dp"
android:drawableTop="@mipmap/dialog_menu_case"
android:paddingBottom="13.5dp"
android:paddingEnd="36dp"
android:paddingLeft="36dp"
android:paddingRight="36dp"
android:paddingTop="20dp"
android:gravity="center_horizontal"
android:text="Case"
android:textColor="#ff666666"
android:textSize="14sp" />
<TextView
android:id="@+id/dialog_menu_help_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@drawable/bg_dialog_menu_item"
android:drawablePadding="13dp"
android:drawableTop="@mipmap/dialog_menu_help"
android:gravity="center_horizontal"
android:paddingBottom="13.5dp"
android:paddingEnd="36dp"
android:paddingLeft="36dp"
android:paddingRight="36dp"
android:paddingTop="20dp"
android:text="Help"
android:textColor="#ff666666"
android:textSize="14sp" />
</LinearLayout>
R.style.menu_dialog
<style name="menu_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
</style>
菜单Dialog 效果图
DialogActivity
public class DialogActivity extends AppCompatActivity implements View.OnClickListener {
private MenuDialog menuDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_dialog);
this.menuDialog = MenuDialog.DialogBuilder.getInstance(this)
.setCaseListenser(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(DialogActivity.this, "case", Toast.LENGTH_SHORT).show();
DialogActivity.this.menuDialog.dismiss();
}
})
.setHelpListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(DialogActivity.this, "Help", Toast.LENGTH_SHORT).show();
DialogActivity.this.menuDialog.dismiss();
}
})
.getDialog();
this.initListeners();
}
private void initListeners() {
this.findViewById(R.id.dialog_custom).setOnClickListener(this);
this.findViewById(R.id.dialog_menu).setOnClickListener(this);
}
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dialog_custom:
CustomDialog.DialogBuilder.getInstance(this)
.setDuration(2000L)
.setContent("CustomDialog")
.setCanceledOnTouchOutside(false)
.setCallback(new CustomDialog.DialogCallback() {
@Override
public void onDismiss() {
Toast.makeText(DialogActivity.this, "CustomDialog dismiss", Toast.LENGTH_SHORT).show();
}
})
.getDialog()
.show();
break;
case R.id.dialog_menu:
this.menuDialog.show();
break;
}
}
}
activity_dialog.xml
<?
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="26dp">
<TextView
android:id="@+id/dialog_custom"
style="@style/TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CustomDialog" />
<TextView
android:id="@+id/dialog_menu"
style="@style/TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MenuDialog" />
</LinearLayout>
46.Android 自己定义Dialog的更多相关文章
- Android自己定义dialog中的EditText无法弹出键盘的解决
近期我独立开发的项目<全医会>已经在内測其中了.非常快将会上架到各大应用市场.之前开发的几个项目都由于一些原因没有上架还是比較遗憾的.所以,近期我心情格外的好. 今天在做一个新项目,专为律 ...
- android 自己定义dialog并实现失去焦点(背景透明)的功能
前言:因为在项目中须要用到更新显示动画的需求,所以想到了dialog,自己定义dialog不难.网上教程非常多,可是在实现dialog背景透明的需求时,遇到了一点问题.网上的一些方法在我的机器上并没有 ...
- android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)
自定义一个dialog: 之前有很多博客都有过这方面的介绍,可是个人觉得通常不是很全面,通用性不是很强,一般会定义一个自己的dialog类,然后去使用,难道每一个dialog都要定义一个class吗? ...
- Android Activity模拟dialog
Android项目中很多地方,都会弹出一个弹出框.类似于自己定义的alertDialog,比如微信的退出提示,但由于Dialog的限制,可能不能很完美的实现你的想要的功能,所有研究发现他们这种实现其实 ...
- Android创建自定义dialog方法详解-样式去掉阴影效果
在自定义组件时,从已有组件源码中会很大收获.就拿progressDialog来说 间接父类是dialog,想了解dialog继承结构可以去百度,或者 从构造器来说ProgressDial ...
- Android自己定义DataTimePicker(日期选择器)
Android自己定义DataTimePicker(日期选择器) 笔者有一段时间没有发表关于Android的文章了,关于Android自己定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中.本篇 ...
- Android-它们的定义Dialog
Android-它们的定义Dialog 2014年4月27日 星期天 天气晴朗 心情平静 本篇博文来分享一个也是开发中常常须要用到的功能-自己定义对话框,这里我用到了Android中的图形资源shap ...
- Android学习自定义Dialog
Dialog是Android提供的各种对话框的基类,和上篇的DialogFragment类似.为什么还要介绍Dialog呢,因为DialogFragment只能运行在Android3.0以上的系统中. ...
- Android studio使用android:style/Theme.Dialog报错:You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
查找原因是在activity java代码部分继承了compatactivity public class DialogActivity extends AppCompatActivity 但是在An ...
随机推荐
- Linux shell脚本中 数组的声明:
数组的声明: 1)array[key]=value # array[0]=one,array[1]=two 复制代码 2)declare -a array # array被当作数组名 复制代码 3)a ...
- 关于FLASK WEB开发8d 数据库迁移的问题
首先, 第一步,要删除data-dev.sqlite这个数据库 第二步,进行下面的重建 暂时的解决办法是: python manage.py shell In [2]: from app import ...
- dede其他栏目页的logo没有完整显示怎么办?
在首页完全没有问题,可是点击关于我们.联系我们.加入我们的时候logo图标是缺失的,这时候怎么办? 其实这个是css样式的问题,只要找到相对应页面的css,改一下他们的宽就可以了,如果高不够就自己调整 ...
- Python游戏开发:pygame游戏开发常用数据结构
一.数组与列表 数组可以理解为简化的列表.像我们之前使用的pygame.sprite.Group这样的精灵组,也是一个列表.列表的元素是可变的,它具有添加.删除.搜索.排序等多种方法. 1.一维列表 ...
- LPSTR LPCTSTR
UNICODE:它是用两个字节表示一个字符的方法.比如字符'A'在ASCII下面是一个字符,可'A'在UNICODE下面是两个字符,高字符用0填充,而且汉字'程'在ASCII下面是两个字节,而在UNI ...
- 梦想CAD控件网页版线型
增加线型 主要用到函数说明: _DMxDrawX::AddLinetype 增加一个线型定义.详细说明如下: 参数 说明 BSTR pszName 线型名 BSTR pszLineDefine 线定义 ...
- 16位/32位/64位CPU的位究竟是说啥
平时,我们谈论CPU,都会说某程序是32位编译,可以跑在32位机或64位机,或则是在下载某些开源包时,也分32位CPU版本或64CPU位版本,又或者在看计算机组成相关书籍时,特别时谈到X86 CPU时 ...
- 12Java Bean
Java Bean JavaBean是一种组件体系结构.实际上,JavaBean就是一个Java类,这个类可以重复地使用.我们可以把JavaBean看成是一个黑盒子,即只需要知道其功能而不必管其内部 ...
- spring aop 内部调用问题解决
方法1: 基于 proxy 的 spring aop 带来的内部调用问题可以使用 AopContext.currentProxy() 强转为当前的再调用就可以解决了 例如: 错误用法:public A ...
- form表单传输多余参数
1.使用post提交表单,同时在form的action属性后添加“?参数=参数值”,经验证,可行,但是在浏览器中看不到该参数在form参数中,如下图: 上图未出现courseId属性,form代码如下 ...