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的更多相关文章

  1. Android自己定义dialog中的EditText无法弹出键盘的解决

    近期我独立开发的项目<全医会>已经在内測其中了.非常快将会上架到各大应用市场.之前开发的几个项目都由于一些原因没有上架还是比較遗憾的.所以,近期我心情格外的好. 今天在做一个新项目,专为律 ...

  2. android 自己定义dialog并实现失去焦点(背景透明)的功能

    前言:因为在项目中须要用到更新显示动画的需求,所以想到了dialog,自己定义dialog不难.网上教程非常多,可是在实现dialog背景透明的需求时,遇到了一点问题.网上的一些方法在我的机器上并没有 ...

  3. android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)

    自定义一个dialog: 之前有很多博客都有过这方面的介绍,可是个人觉得通常不是很全面,通用性不是很强,一般会定义一个自己的dialog类,然后去使用,难道每一个dialog都要定义一个class吗? ...

  4. Android Activity模拟dialog

    Android项目中很多地方,都会弹出一个弹出框.类似于自己定义的alertDialog,比如微信的退出提示,但由于Dialog的限制,可能不能很完美的实现你的想要的功能,所有研究发现他们这种实现其实 ...

  5. Android创建自定义dialog方法详解-样式去掉阴影效果

    在自定义组件时,从已有组件源码中会很大收获.就拿progressDialog来说     间接父类是dialog,想了解dialog继承结构可以去百度,或者    从构造器来说ProgressDial ...

  6. Android自己定义DataTimePicker(日期选择器)

    Android自己定义DataTimePicker(日期选择器)  笔者有一段时间没有发表关于Android的文章了,关于Android自己定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中.本篇 ...

  7. Android-它们的定义Dialog

    Android-它们的定义Dialog 2014年4月27日 星期天 天气晴朗 心情平静 本篇博文来分享一个也是开发中常常须要用到的功能-自己定义对话框,这里我用到了Android中的图形资源shap ...

  8. Android学习自定义Dialog

    Dialog是Android提供的各种对话框的基类,和上篇的DialogFragment类似.为什么还要介绍Dialog呢,因为DialogFragment只能运行在Android3.0以上的系统中. ...

  9. 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 ...

随机推荐

  1. mysql中数据库的设计

      软件开发流程(CMMI): 1):项目启动; 2):项目计划: 3):需求分析; 需要得到的结果是什么? 4):系统设计;         该怎么做? 5):系统开发; 6):系统测试; 7):系 ...

  2. Intellij使用心得(四) -- 导入Eclipse的代码格式化文件

    https://my.oschina.net/flashsword/blog/137598

  3. PostgreSQL 流复制+高可用

    QA PgPool-II 同步 Postgresql X1 服务器准备 192.168.59.121 PostgreSQL10 192.168.59.120 PGPool-II 3.7 X2 安装Po ...

  4. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  5. Farseer.net轻量级开源框架 中级篇:事务的使用

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: Where条件的终极使用 下一篇:Farseer.net轻量级开源框架 中级篇: ...

  6. PHP7中session_start 使用注意事项,会导致浏览器刷时页面数据不更新

    //PHP7中session_start 使用注意事项, session_start([ 'cache_limiter' => 'private', //在读取完毕会话数据之后马上关闭会话存储文 ...

  7. MFC_2.8 使用状态栏工具栏

    使用状态栏工具栏 1.资源-添加-TOOLBAR 画图标.画了一个,第二个会出来. 2.头文件添加成员 CToolBar m_ToolBar; CStatusBar m_StatusBar; 3.初始 ...

  8. tee命令用法

    用途说明 在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我们就不能看到输出了,如果我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee命令 ...

  9. js实现字符串反转

    方案1: var str = "abcdef"; console.log( str.split("").reverse().join("") ...

  10. TWaver HTML5之树形布局

    转眼间春节假期已经过完,作为一个职业的程序猿,不知道大家有没有这样的感觉,一天不碰电脑,总觉得生活少点什么.今天是春节后上班的第三天,给大家分享一下我们前段时间的一个需求,需求是这样的:界面中的网元分 ...