Android自定义弹出框dialog效果
作者:灬布衣丶公爵丨 时间:2023-06-29 14:14:32
项目要用到弹出框,还要和苹果的样式一样(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
0
投稿
猜你喜欢
Java Web实现简易图书管理系统
2023-12-17 21:48:34
高斯混合模型与EM算法图文详解
2022-10-02 12:05:02
IDEA中多行注释及取消注释的快捷键分享
2022-02-01 06:31:17
C语言实现仓库物资管理系统
2023-04-23 12:30:51
旧项目升级新版Unity2021导致Visual Studio无法使用的问题
2023-12-28 21:51:26
Mybatis 如何开启控制台打印sql语句
2023-08-02 02:01:53
Android权限如何禁止以及友好提示用户开通必要权限详解
2023-10-09 04:29:41
Android离线Doc文档访问速度慢的有效解决方法
2021-06-27 02:10:39
android操作XML的几种方法总结
2021-08-10 06:06:16
基于c# Task自己动手写个异步IO函数
2021-08-06 12:07:56
C#实现根据银行卡卡号判断银行名
2021-08-21 07:14:00
SpringBoot 文件上传和下载的实现源码
2021-05-28 14:12:46
C#实现自由组合本地缓存、分布式缓存和数据查询
2021-06-13 00:56:11
Android中GPS定位的用法实例
2021-07-26 23:26:29
Android数据持久化之Preferences机制详解
2021-09-04 23:42:32
C语言函数声明以及函数原型超详细讲解示例
2023-03-31 03:12:02
Android搜索结果显示高亮实例(有数据滑动底部自动刷新)
2021-09-25 22:55:07
Android 读取文件内容实现方法总结
2022-09-15 12:57:56
android通过google api获取天气信息示例
2023-10-18 15:44:22
MyBatis-plus中的模糊查询解读
2022-06-16 08:27:03