Android自定义弹出框dialog效果
作者:灬布衣丶公爵丨 发布时间:2023-06-29 14:14:32
标签:Android,弹出框,dialog
项目要用到弹出框,还要和苹果的样式一样(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();


猜你喜欢
- 概述从今天开始, 小白我将带大家开启 Java 数据结构 & 算法的新篇章.贪心算法贪心算法 (Greedy Algorithm)
- package com.ppmeet; import java.io.IOException; import and
- springboot的最强大的就是那些xxxAutoconfiguration,但是这些xxxAutoConfiguration又依赖那些s
- 本文实例讲述了Java求解两个非负整数最大公约数算法。分享给大家供大家参考,具体如下:代码功能:1.Java实现(完整源码附测试用例);2.
- 最近学习Spring,一直不太明白Srping的切面编程中的的argNames的含义,经过学习研究后,终于明白,分享一下需要监控的类:pac
- 【前言】AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统
- 1、前言C++虚继承的内存模型是一个经典的问题,其具体实现依赖于编译器,可能会出现较大差异,但原理和最终的目的是大体相同的。本文将对g++中
- 本文讲解2点:1. fastjson生成和解析json数据(举例:4种常用类型:JavaBean,List<JavaBean>,
- 在界面设计中,一个容器要放置许多组件,为了美观,为组件安排在容器中的位置,这就是布局设计。java.awt中定义了多种布局类,每种布局类对应
- 如下,可以使用C#的Replace()方法来替换,但有一点需要注意的是backslash(反斜杠)是特殊字符。string s = &quo
- 程序如下:View Code /* * Hanoi塔游戏 问题描述: * 汉诺塔:汉诺塔(又称河内塔)问
- spring batch简介spring batch是spring提供的一个数据处理框架。企业域中的许多应用程序需要批量处理才能在关键任务环
- 今天来了一个问题:软键盘无法弹出。分析后是因为系统判断当前有外接硬键盘,就会隐藏软键盘。但实际情况并不是这么简单,该问题只有在特定条件下偶现
- 1、图的定义我们知道,前面讨论的数据结构都有一个框架,而这个框架是由相应的算法实现的,比如二叉树搜索树,左子树上所有结点的值均小于它的根结点
- 前言大家都知道在Android WebView使用中,经常需要H5页面和Native页面进行交互,比如在网页上点击分享按钮,调用本地分享接口
- 简介:本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件思路:主要重写Recyclerview
- 开发环境win10Android Studio效果用于多级菜单展示,或选择。如 每个省,市,县;如 树木的病虫害;关键代码 @overrid
- 一条SQL使用两个foreach的问题未修改前的 SQL 语句<select id="findQuestionType_3_
- MyBatis中PageHelper不生效今天使用pageHelper,发现设置了PageHelper.startPage(page, pa
- 前言本文主要介绍了关于java结合keytool实现非对称签名和验证的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍