自定义DialogAlert消息框
效果:

一、新建类CommomDialog 继承Dialog
public class CommomDialog extends Dialog implements View.OnClickListener{
private TextView contentTxt;
private TextView titleTxt;
private TextView submitTxt;
private TextView cancelTxt;
private Context mContext;
private String content;
private OnCloseListener listener;
private String positiveName;
private String negativeName;
private String title;
public CommomDialog(Context context) {
super(context);
this.mContext = context;
}
public CommomDialog(Context context, String content) {
super(context, R.style.dialog);
this.mContext = context;
this.content = content;
}
public CommomDialog(Context context, int themeResId, String content) {
super(context, themeResId);
this.mContext = context;
this.content = content;
}
public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) {
super(context, themeResId);
this.mContext = context;
this.content = content;
this.listener = listener;
}
protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
this.mContext = context;
}
public CommomDialog setTitle(String title){
this.title = title;
return this;
}
public CommomDialog setPositiveButton(String name){
this.positiveName = name;
return this;
}
public CommomDialog setNegativeButton(String name){
this.negativeName = name;
return this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_commom);
setCanceledOnTouchOutside(false);
initView();
}
private void initView(){
contentTxt = (TextView)findViewById(R.id.content);
titleTxt = (TextView)findViewById(R.id.title);
submitTxt = (TextView)findViewById(R.id.submit);
submitTxt.setOnClickListener(this);
cancelTxt = (TextView)findViewById(R.id.cancel);
cancelTxt.setOnClickListener(this);
contentTxt.setText(content);
if(!TextUtils.isEmpty(positiveName)){
submitTxt.setText(positiveName);
}
if(!TextUtils.isEmpty(negativeName)){
cancelTxt.setText(negativeName);
}
if(!TextUtils.isEmpty(title)){
titleTxt.setText(title);
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.cancel:
if(listener != null){
listener.onClick(this, false);
}
this.dismiss();
break;
case R.id.submit:
if(listener != null){
listener.onClick(this, true);
}
break;
}
}
public interface OnCloseListener{
void onClick(Dialog dialog, boolean confirm);
}
}
二、新建dialog_commom.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:background="@drawable/bg_round_white"
android:orientation="vertical" > <TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="12dp"
android:layout_marginTop="12dp"
android:text="提示"
android:textSize="16sp"
android:textColor="@color/black"/> <TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:lineSpacingExtra="3dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="30dp"
android:text="签到成功,获得200积分"
android:textSize="12sp"
android:textColor="@color/font_common_1"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/commom_background"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"> <TextView
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_dialog_left_white"
android:layout_weight="1.0"
android:gravity="center"
android:text="取消"
android:textSize="12sp"
android:textColor="@color/font_common_2"/> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/commom_background"/> <TextView
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_dialog_right_white"
android:gravity="center"
android:layout_weight="1.0"
android:text="确定"
android:textSize="12sp"
android:textColor="@color/font_blue"/> </LinearLayout> </LinearLayout>
三、在drawable下新建三个xml
bg_dialog_left_white.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/white" />
<corners android:bottomLeftRadius="8dp" />
</shape>
bg_dialog_right_white.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:bottomRightRadius="8dp" />
</shape>
bg_round_white.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/white" />
<corners android:radius="8dp" />
</shape>
四、在styles.xml添加
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!--边框-->
<item name="android:windowIsFloating">true</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item>
<!--半透明-->
<item name="android:windowNoTitle">true</item>
<!--无标题-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--背景透明-->
<item name="android:backgroundDimEnabled">true</item>
<!--模糊--> </style>
五、在colors.xml中添加
<color name="mainColor">#573567</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="font_gray_b">#d4d4d3</color> <color name="font_tab_1">#42369a</color>
<color name="font_tab_0">#b1b1b1</color> <color name="font_common_1">#424242</color>
<color name="font_common_2">#a1a1a1</color>
<color name="font_blue">#42369a</color> <color name="font_green">#00cccc</color> <color name="commom_background">#f3f3f3</color>
六、触发
new CommomDialog(Home.this, R.style.dialog, "确认退出此程序?", new CommomDialog.OnCloseListener() {
@Override
public void onClick(Dialog dialog, boolean confirm) {
if (confirm) {
ActivityCollector.FinishAll();
dialog.dismiss();
}
}
}).setTitle("提示").show();
作者发布的源码:https://github.com/xiaoxiaoqingyi/mine-android-repository
自定义DialogAlert消息框的更多相关文章
- Vue 自定义全局消息框组件
消息弹框组件,默认3秒后自动关闭,可设置info/success/warning/error类型 效果图: 文件目录: Message.vue <template> <transit ...
- WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 自定义 ...
- 【转】WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 自定义Window窗体样式: 基于自定义窗体实现自定义MessageB ...
- 【C#】分享一个可携带附加消息的增强消息框MessageBoxEx
--------------201507160917更新--------------- 无意中发现标准消息框在Windows7是有声音的,只是在Windows server 2008(R2)无声,而我 ...
- AloneJs.msgbox() —— 弹出消息框
一.引用 <link href="https://cdn.suziyun.com/alonejs.min.css" rel="stylesheet" /& ...
- C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色
在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示 和错误等类别.为了更好地区分不同类型的日志,我们需要使 ...
- EasyUI Messager 消息框
通过 $.messager.defaults 重写默认的 defaults. 消息框(messager)提供不同样式的消息框,包括警示(alert).确认(confirm).提示(prompt).进展 ...
- Delphi 7中的四种消息框
Delphi中平常使用的消息框有四种形式,有ShowMessage.MessageDlg.Application.MessageBox.MessageBox.下面来深入了解下这四种形式的实现和使用.1 ...
- 【Web】一个非常简单的移动web消息框
适用:h5+jquery,移动网页最佳 最近在写个简单的公众号页面,前端验证时有些信息要提示,很简单的需求实在不想找啥现成的轮子,又不至于用alert这么粗暴,遂写了个非常简单的消息框,效果如图: 特 ...
随机推荐
- Eclipse添加struts2
参照:http://jingyan.baidu.com/article/915fc414fd94fb51394b208e.html 一.插件下载:http://struts.apache.org/do ...
- JavaScript创建对象的方法
显示在浏览器中的控制台中. <script type="text/javascript"> //这个工厂方法返回一个新的"范围对象" functio ...
- 深入理解Java内部类
内部类就是定义在一个类中的另外一个类,是一种从属关系.在没有实际了解内部类之前,我始终困惑,为什么要在一个类中定义另外一个类,这不是增加代码结构复杂度么?现在才大致能知道这种设计的优势是大于 ...
- 用caffe一步一步实现人脸检测
学习深度学习已有一段时间了,总想着拿它做点什么,今天终于完成了一个基于caffe的人脸检测,这篇博文将告诉你怎样通过caffe一步步实现人脸检测.本文主要参考唐宇迪老师的教程,在这里感谢老师的辛勤付出 ...
- Openfire3.9.1+jdk1.7导入到eclipse中
Openfire3.9.1+jdk1.7导入到eclipse中 写这篇文章,也是记录一下自己几晚上的辛苦,因为作为新手在网上看了很多的资料,但是按照他们的我总是出不来,跟他们描述的不一致,可能是环境问 ...
- 各种demo:css实现三角形,css大小梯形,svg使用
各种demo: 1.css实现正方形 思路:width为0:height为0:使用boder-width为正方形的边长的一半,不占任何字节:border-style为固体:border-color为正 ...
- form表单提交图片禁止跳转
问题: 最近在做项目时,遇到上传图片需求,且在不跳转的情况下获取到返回信息 思路: 1. 使用ajax发送异步请求,经多次测试,最终以失败告终 2. iframe 禁止跳转(未尝试) 3. 修改fo ...
- OPNET中FIN,FOUT以及FRET的作用 分类: opnet 2014-05-12 16:07 144人阅读 评论(0) 收藏
为了使一个用户定义的函数被执行,该函数必须与一个特殊的堆栈跟踪代码相连.堆栈跟踪技术靠在函数的入口点和出口点插入预处理器宏指令完成(一个函数只有一个入口点,但可以有多个出口点(由C语言的return声 ...
- 初学Python(四)——set
初学Python(四)——set 初学Python,主要整理一些学习到的知识点,这次是set. # -*- coding:utf-8 -*- #先来看数组和set的差别 d=[1,1,2,3,4,5] ...
- Android学习笔记-App初始启动界面实现
android手机上的很多应用程序启动时都会先显示一个图片,作为该应用程序的开始,该图片转瞬即逝.这个图片一般都会用应用的图标,作为广告来用. 例如: 它的实现方式很简单,我们以一个测试APP为例,介 ...