项目要用到弹出框,还要和苹果的样式一样(Android真是没地位),所以就自己定义了一个,不是很像(主要是没图),但是也还可以。

废话不多说了,直接上代码

1、先看布局文件

<?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:padding="20dp"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@drawable/custom_dialog_background"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"> <TextView
android:id="@+id/tv_title_custom_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="提醒"
android:textColor="#000"
android:textSize="18dp" /> <TextView
android:id="@+id/tv_message_custom_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="您确定要取消订单吗" />
</LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="20dp"
android:background="#dfdfdf" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/btn_negative_custom_dialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="取消"
android:textColor="@android:color/holo_blue_dark" /> <View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#dfdfdf" /> <Button
android:id="@+id/btn_positive_custom_dialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="确定"
android:textColor="@android:color/holo_blue_dark" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

2、集成dialog重写了一下

package newair.com.storelibrary.ui.custom.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView; import newair.com.storelibrary.R; /**
* Created by ouhimehime on 16/4/22.
* ---------自定义提示框-----------
*/
public class CustomDialog extends Dialog { public CustomDialog(Context context) {
super(context);
} public CustomDialog(Context context, int themeResId) {
super(context, themeResId);
} protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
} public static class Builder {
private Context context;
private String title; //标题
private String message;//提示消息
private String negative_text;//消极的
private String positive_text;//积极的
private DialogInterface.OnClickListener negativeListener;//消极的监听
private DialogInterface.OnClickListener positiveListener;//积极的监听 public Builder(Context context) {
this.context = context;
} public Builder setTitle(String title) {
if (title == null) {
this.title = "提醒";
}
this.title = title;
return this;
} public Builder setMessage(String message) {
if (message == null) {
this.message = "您没有填写提示信息哦";
}
this.message = message;
return this;
} public Builder setNegativeButton(String negative_text, DialogInterface.OnClickListener negativeListener) {
if (negative_text == null) {
this.negative_text = "取消";
}
this.negative_text = negative_text;
this.negativeListener = negativeListener; return this;
} public Builder setPositionButton(String positive_text, DialogInterface.OnClickListener positiveListener) {
if (positive_text == null) {
this.positive_text = "确定";
}
this.positive_text = positive_text;
this.positiveListener = positiveListener; return this;
} private TextView tv_title_custom_dialog; //标题
private TextView tv_message_custom_dialog;//提示信息
private Button btn_negative_custom_dialog;//消极
private Button btn_positive_custom_dialog;//积极 public CustomDialog create() {
final CustomDialog dialog = new CustomDialog(context);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_custom_style_layout, null);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句,取消原来的标题栏,没加这句之前,发现在三星的手机上会有一条蓝色的线
// dialog.addContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
tv_title_custom_dialog = (TextView) view.findViewById(R.id.tv_title_custom_dialog);
tv_message_custom_dialog = (TextView) view.findViewById(R.id.tv_message_custom_dialog);
btn_negative_custom_dialog = (Button) view.findViewById(R.id.btn_negative_custom_dialog);
btn_positive_custom_dialog = (Button) view.findViewById(R.id.btn_positive_custom_dialog);
tv_title_custom_dialog.setText(title);
tv_message_custom_dialog.setText(message);
btn_negative_custom_dialog.setText(negative_text);
btn_positive_custom_dialog.setText(positive_text);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
btn_negative_custom_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
}
});
btn_positive_custom_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positiveListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
}
});
return dialog;
}
}
}

3、使用起来和系统的用法一样

CustomDialog.Builder builder = new CustomDialog.Builder(this);
builder.setTitle("购物提醒")
.setMessage("我是提示信息,大家好好")
.setNegativeButton("再看看", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(GoodsListActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
}
})
.setPositionButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(GoodsListActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
}
})
.create()
.show();

Android--自定义弹出框-自定义dialog的更多相关文章

  1. android自定义弹出框样式实现

    前言: 做项目时,感觉Android自带的弹出框样式比较丑,很多应用都是自己做的弹出框,这里也试着自己做了一个. 废话不说先上图片: 实现机制 1.先自定义一个弹出框的样式 2.自己实现CustomD ...

  2. .NET MVC 学习笔记(四)— 基于Bootstarp自定义弹出框

    .NET MVC 学习笔记(四)—— 基于Bootstarp自定义弹出框 转载自:https://www.cnblogs.com/nele/p/5327380.html (function ($) { ...

  3. js自定义弹出框

    js自定义弹出框: 代码如下 <html> <head><title>自定义弹出对话框</title> <style type ="te ...

  4. react native仿微信性别选择-自定义弹出框

    简述 要实现微信性别选择需要使用两部分的技术: 第一.是自定义弹出框: 第二.单选框控件使用: 效果 实现 一.配置弹出框 弹出框用的是:react-native-popup-dialog(Git地址 ...

  5. android 自定义弹出框AlertDialog ,很炫的哦

      于是就小小的模仿了下自己写了这个这样的效果,主要代码如下:dlg = new AlertDialog.Builder(context).create();dlg.show();dlg.getWin ...

  6. bootstrap插件bootbox参数和自定义弹出框宽度设置

    插件官方地址:http://bootboxjs.com/ alert: 1 bootbox.alert("Hello world!", function() {}); dialog ...

  7. jquery实现自定义弹出框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 自定义弹出框基于zepto 记得引入zepto

    html <!DOCTYPE html> <html> <meta charset="utf-8"> <title></tit ...

  9. unity 之 自定义弹出框

    一.弹出框的搭建: 布局如图:Message为整个父物体,并且添加UiMessage代码.panel为遮罩. MessageBox为整个提示框,Panel为标题,ok为确定按钮,cancel为取消按钮 ...

随机推荐

  1. Quartz.NET基础知识概述

    Quartz.NET是什么 由于我现在使用的Quartz.NET2.2版本,相对2.x变化不大,主要是相对于1.x更新了很多东西,如下基础知识摘录网络. Quartz.NET是一个开源的作业调度框架, ...

  2. This Gradle plugin requires Studio 3.0 minimum

    从github上下载的项目遇到一个问题:Error:This Gradle plugin requires Studio 3.0 minimum 意思就是说studio版本不高,导入的项目的版本是3. ...

  3. 在vue中使用vuex 一个简单的实例

    1.安装vuex:npm install vuex --save 2.在main.js文件中引入vuex (请忽略其它代码) 3.建一个vuex文件夹,然后在建一个store.js(这两个文件名字可以 ...

  4. 如何测试你给客户端app开的接口

    这里介绍一款工具用于测试后台给客户端开的接口. 采用http或者https 采用表单或者json格式 这款工具之前是谷歌浏览器的一款插件,后来出现了各个平台的客户端.非常实用. 名叫postman 官 ...

  5. 2015年第六届蓝桥杯C/C++程序设计本科B组决赛 完美正方形

    完美正方形 如果一些边长互不相同的正方形,可以恰好拼出一个更大的正方形,则称其为完美正方形.历史上,人们花了很久才找到了若干完美正方形.比如:如下边长的22个正方形 2 3 4 6 7 8 12 13 ...

  6. Flux --> Redux --> Redux React 基础实例教程

    本文的目的很简单,介绍Redux相关概念用法 及其在React项目中的基本使用 假设你会一些ES6.会一些React.有看过Redux相关的文章,这篇入门小文应该能帮助你理一下相关的知识 一般来说,推 ...

  7. PHP7最高性能优化建议

    PHP7已经发布了, 作为PHP10年来最大的版本升级, 最大的性能升级, PHP7在多放的测试中都表现出很明显的性能提升, 然而, 为了让它能发挥出最大的性能, 我还是有几件事想提醒下. PHP7 ...

  8. ActiveMQ P2P版的HelloWorld

    1.2 JMS应用程序接口 ConnectionFactory: 用户用来创建到JMS提供者的连接的被管对象.JMS客户通过可移植的接口访问连接,这样当下层的实现改变时,代码不需要进行修改. 管理员 ...

  9. unity中 TextMeshPro 的常用标签

    这个第二和第三个写反了. 例子10中的123标签需要用到另一个字体,详情看 TextMeshPro 的官方示例10.

  10. MySQL闪退问题的解决

    刚刚学习了数据库,并且安装了MySQL,正当高兴之余,发现我的MySQL出现了闪退的显现.上网搜了好久的解决方案.最后解决了这个问题,也舒心了. 问题从这里开始: 接着我打开MySQL,寻思能不能用, ...