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 ...
随机推荐
- c# Queue实现生产者(Producer)消费者(Consumer)模式
我们在开发过程中经常会遇到需要从一个地方不断获取数据然后又需要交给另一个线程对数据进行二次加工的情况,这种场景适合使用生产者-消费者模式. Demo展示 //中间的容器 public static c ...
- 可以在一个html的文件当中读取另一个html文件的内容
1.IFrame引入,看看下面的代码 <IFRAME NAME="content_frame" width=100% height=30 marginwidth=0 marg ...
- PyCharm使用指南及更改Python pip源为国内豆瓣
PyCharm基本使用 1.在PyCharm下为python项目配置python本地解释器 setting-->Project:pycharm workspace-->Project In ...
- 编写Java脚本统计工程代码总行数
在新公司工作将近一年了,一直独自一人负责服务端集群的运维和代码的编写.不知不觉从一个Project发展到了七八个Project. 看着越来越多的代码,今天突然想统计一下一共写了多少代码.[这里只统计完 ...
- jstree中json data 的生成
jstree中json data 的生成 jstree官网上给出的json数据格式是这样的: <span style="font-size:14px;">// A ...
- POJ_1611_The Suspect
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 25149 Accepted: 12329 De ...
- MFC_2.8 使用状态栏工具栏
使用状态栏工具栏 1.资源-添加-TOOLBAR 画图标.画了一个,第二个会出来. 2.头文件添加成员 CToolBar m_ToolBar; CStatusBar m_StatusBar; 3.初始 ...
- ThinkPHP---thinkphp模型(M)
(1)配置数据库连接 数据库的连接配置可以在系统配置文件ThinkPHP/Conf/convention.php中找到 /* 数据库设置 */ 'DB_TYPE' => '', // 数据库类型 ...
- 微服务网关从零搭建——(八)Ocelot网关中加入skywalking APM
准备工作 一.下载skywalking 本例使用的是 注: 1.解压后执行完2,3步骤后运行\bin\startup.bat 2.默认后台端口为8080 如需修改则修改\webapp\webapp.y ...
- 浅谈java浅拷贝和深拷贝
前言:深拷贝和浅拷贝的区别是什么? 浅拷贝:被复制的对象的所有变量都含有原来对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.换言之, 浅拷贝仅仅复制所考虑的对象,而不复制它所引用的对象.深拷 ...