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. Java开发笔记(九十二)文件通道的基本用法

    前面介绍的各色流式IO在功能方面着实强大,处理文件的时候该具备的操作应有尽有,可流式IO在性能方面不尽如人意,它的设计原理使得实际运行效率偏低,为此从Java4开始增加了NIO技术,通过全新的架构体系 ...

  2. npm install的时候报错 npm err code 1

    在学习vue的时候,npm install的时候报错  npm err code 1,当时很郁闷,是‘vue init webpack my-project’命令新建的模版项目 ,怎么会报错,第一次遇 ...

  3. setTimeout 0

    setTimeout 0 就是把事件放到下一次事件循环时调用,至少要一个时钟之后: ); console.log('this should before setTimeout 0'); var i=0 ...

  4. 梦想Android版CAD控件2018.10.12更新

    下载地址: http://www.mxdraw.com/ndetail_10106.html 1. 增加读写对象扩展字典功能 2. 修改样条线显示错误 3. 修改shx文字显示错误 4. 增加向量运算 ...

  5. Robot Framework(九) 执行测试用例——基本用法

    3.1基本用法 Robot Framework测试用例从命令行执行,默认情况下,最终结果是XML格式的输出文件和HTML 报告和日志.执行后,可以组合输出文件,然后使用rebot工具进行后处理. 3. ...

  6. MySQL:INSERT ... UPDATE

    在 INSERT 语句末尾指定ON DUPLICATE KEY UPDATE时,如果插入的数据会导致表中的 UNIQUE 索引或 PRIMARY KEY 出现重复值,则会对导致重复的数据执行 UPDA ...

  7. Duboo学习-SPI

    待补充 现将Dubbo-SPI相关源码流程图更新

  8. C++中重载,重写,隐藏的区别

    重载: 重载是指在同一个作用域下,函数的函数名相同,但是函数参数的个数,或者参数的类型,参数的顺序不同.这时函数之间就构成了重载关系,这里需要注意的是,如果函数的参数列表完全相同,仅仅是返回值类型不同 ...

  9. [Algorithm] 11. Linked List Cycle

    Description Given a linked list, determine if it has a cycle in it. To represent a cycle in the give ...

  10. 使用js将Unix时间戳转换为普通时间

    var unixtime=1358932051;formatTime (time) { let unixtime = time let unixTimestamp = new Date(unixtim ...