博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开发之自定义对话框
阅读量:4880 次
发布时间:2019-06-11

本文共 6327 字,大约阅读时间需要 21 分钟。

由于系统自带的对话框不好看,于是本人就自定义了一个对话框,以后有类似的就可以直接使用啦。先上效果图:

这里写图片描述

1,布局文件dialog_clear_normal.xml

2,文件夹values/colors.xml

#3F51B5
#303F9F
#FF4081
#ffffff
#000000
#222222
#00000000
#48a1ff
#dddddd
#aaaaaa
#80dddddd
#80aaaaaa
#80222222
#40ffffff
#0000ff

3,文件夹values/styles.xml

4,文件夹drawable/的一些xml文件:

这里写图片描述
1)

2)

3)

4)

5)

6)

5,自定义一个对话框的类:

package com.gyq.dialog;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; /** * ${DESC} * author: gyq * create at 2017/2/7 15:33 */ public class CustomDialog extends Dialog { public CustomDialog(Context context) { super(context); } public CustomDialog(Context context, int theme) { super(context, theme); } public static class Builder { private Context context; private String title; private String message; private String positiveButtonText; private String negativeButtonText; private View contentView; private DialogInterface.OnClickListener positiveButtonClickListener; private DialogInterface.OnClickListener negativeButtonClickListener; public Builder(Context context) { this.context = context; } public Builder setMessage(String message) { this.message = message; return this; } /** * Set the Dialog message from resource * * @param * @return */ public Builder setMessage(int message) { this.message = (String) context.getText(message); return this; } /** * Set the Dialog title from resource * * @param title * @return */ public Builder setTitle(int title) { this.title = (String) context.getText(title); return this; } /** * Set the Dialog title from String * * @param title * @return */ public Builder setTitle(String title) { this.title = title; return this; } public Builder setContentView(View v) { this.contentView = v; return this; } /** * Set the positive button resource and it's listener * * @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; } public CustomDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final CustomDialog dialog = new CustomDialog(context,R.style.Dialog); View layout = inflater.inflate(R.layout.dialog_clear_normal, null); dialog.addContentView(layout, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); // set the dialog title ((TextView) layout.findViewById(R.id.dialog_title)).setText(title); // set the confirm button if (positiveButtonText != null) { ((Button) layout.findViewById(R.id.btn_dialog_ok)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((Button) layout.findViewById(R.id.btn_dialog_ok)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.btn_dialog_ok).setVisibility( View.GONE); } // set the cancel button if (negativeButtonText != null) { ((Button) layout.findViewById(R.id.btn_dialog_cancel)) .setText(negativeButtonText); if (negativeButtonClickListener != null) { ((Button) layout.findViewById(R.id.btn_dialog_cancel)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.btn_dialog_cancel).setVisibility( View.GONE); } // set the content message if (message != null) { ((TextView) layout.findViewById(R.id.dialog_message)).setText(message); } else if (contentView != null) { // if no message set // add the contentView to the dialog body ((LinearLayout) layout.findViewById(R.id.dialog_content)) .removeAllViews(); ((LinearLayout) layout.findViewById(R.id.dialog_content)).addView( contentView, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); } dialog.setContentView(layout); return dialog; } } }

7,使用这个dialog:

CustomDialog.Builder builder = new CustomDialog.Builder(this);                builder.setTitle("提示");                builder.setMessage("你确定要清空么?");                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show();

小结:这个自定义的对话宽,点击外面也会消失。有阴影部分,效果还是蛮好看的。

转载于:https://www.cnblogs.com/Free-Thinker/p/8877107.html

你可能感兴趣的文章
nl命令
查看>>
如何使用jQuery $.post() 方法实现前后台数据传递
查看>>
Using Flash Builder with Flash Professional
查看>>
jsp/post中文乱码问题
查看>>
C# 插入或删除word分页符
查看>>
数据库数据的查询----连接查询
查看>>
找不到可安装的ISAM ,asp.net读取数据丢失,解决的一列里有字符与数字的
查看>>
Java学习笔记三(对象的基本思想一)
查看>>
Java程序(文件操作)
查看>>
KMP算法 最小循环节 最大重复次数
查看>>
Proving Equivalences (强连通,缩点)
查看>>
Period (KMP算法 最小循环节 最大重复次数)
查看>>
sgu 103. Traffic Lights
查看>>
poj 3621 Sightseeing Cows
查看>>
hdu 3666 THE MATRIX PROBLEM
查看>>
TopCoder SRM 176 Deranged
查看>>
java 内存模型
查看>>
Javascript中数组与字典(即map)的使用
查看>>
C++不完整的类型
查看>>
memcached(十三)注意事项
查看>>