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

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

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

这里写图片描述

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();

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

你可能感兴趣的文章
Add Two Numbers
查看>>
java基础----泛型!
查看>>
Unicode
查看>>
用VS2010编C#程序扫盲
查看>>
【错排问题】【HDU2048】神、上帝以及老天爷
查看>>
InnoDB MyISAM区别及优化(摘录)
查看>>
关于伪造IP地址的疑问
查看>>
IE兼容问题,各类css hack代码(亲测有效)
查看>>
CCIE学习笔记 ----BGP
查看>>
mysql导入导出
查看>>
sql语句-5-联接组合查询
查看>>
ccf-20171203 Crontab问题
查看>>
t-sql对被除数为0&除数小于被除数结果为0&除法保留2位小数的处理
查看>>
HTTP协议初步认识
查看>>
How to Recover an SD Card With Linux
查看>>
POJ1942
查看>>
Angular2组件开发—模板语法(六)
查看>>
MongoDB基于GridFS管理文件
查看>>
webpack.config.js====图片处理
查看>>
变量应用:页面传Axure变量值
查看>>