Android学习07
自定义Dialog

创建一个Java类CustomDialog继承Dialog。
package com.example.helloworld.widget; import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView; import androidx.annotation.NonNull; import com.example.helloworld.R; public class CustomDialog extends Dialog implements View.OnClickListener { private TextView mTvTitle,mTvMessage,mTvCancel,mTvConfirm;
private String title,message,cancel,confirm;
private IOnCancelListener cancelListener; //取消按钮被点击了的监听器
private IOnConfirmlListener confirmlListener; //确定按钮被点击了的监听器 public CustomDialog(@NonNull Context context) {
super(context);
}
public CustomDialog(@NonNull Context context,int themeId) {
super(context,themeId);
} public CustomDialog setTitle(String title) {
this.title = title;
return this;
} public CustomDialog setMessage(String message) {
this.message = message;
return this;
} public CustomDialog setCancel(String cancel,IOnCancelListener listener) {
this.cancel = cancel;
this.cancelListener = listener;
return this;
} public CustomDialog setConfirm(String confirm,IOnConfirmlListener listener) {
this.confirm = confirm;
this.confirmlListener = listener;
return this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_custom_dialog);
//空白处不能取消动画
setCanceledOnTouchOutside(false); //设置宽度
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p =getWindow().getAttributes();
Point size = new Point();
d.getSize(size);
p.width = (int)(size.x * 0.8); //设置dialog的宽度为当前手机屏幕的宽度*0.8
getWindow().setAttributes(p); mTvTitle = findViewById(R.id.tv_title);
mTvMessage = findViewById(R.id.tv_message);
mTvCancel = findViewById(R.id.tv_cancel);
mTvConfirm = findViewById(R.id.tv_confirm);
if (!TextUtils.isEmpty(title)){
mTvTitle.setText(title);
}
if (!TextUtils.isEmpty(message)){
mTvMessage.setText(message);
}
if (!TextUtils.isEmpty(cancel)){
mTvCancel.setText(cancel);
}
if (!TextUtils.isEmpty(confirm)){
mTvConfirm.setText(confirm);
}
//设置取消和确定的监听事件
mTvCancel.setOnClickListener(this);
mTvConfirm.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_cancel:
if (cancelListener!=null){
cancelListener.onCancel(this);
}
break;
case R.id.tv_confirm:
if (confirmlListener!=null){
confirmlListener.onConfirm(this);
}
break;
}
}
//回调接口
public interface IOnCancelListener{
void onCancel(CustomDialog dialog);
}
public interface IOnConfirmlListener{
void onConfirm(CustomDialog dialog);
}
}
activity_custom.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:orientation="vertical"
android:padding="15dp"> <Button
android:id="@+id/btn_custom_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义Dialog"
android:textAllCaps="false"/> </LinearLayout>
在drawable中新建bg_custom_dialog.xml设置边框:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<solid android:color="#ffffff" />
<stroke
android:width="0.8dp"
android:color="#ffffff" />
<!-- 圆角 -->
<corners android:radius="6dp" />
</shape>
新建layout文件,layout_custom_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="@drawable/bg_custom_dialog"> <TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000"
android:text="提示"
android:textStyle="bold"
android:layout_marginTop="15dp"/> <TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000"
android:text="删除?"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#3CA9C4"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"> <TextView
android:id="@+id/tv_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="取消"
android:textSize="20sp"
android:textColor="#3CA9C4"
android:gravity="center"/> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#3CA9C4"/> <TextView
android:id="@+id/tv_confirm"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="确定"
android:textSize="20sp"
android:textColor="#3CA9C4"
android:gravity="center"/> </LinearLayout> </LinearLayout>
CustomActivity:
package com.example.helloworld; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; import com.example.helloworld.widget.CustomDialog; public class CustomDialogActivity extends AppCompatActivity { private Button mBtnDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom);
mBtnDialog = findViewById(R.id.btn_custom_dialog);
mBtnDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CustomDialog customDialog = new CustomDialog(CustomDialogActivity.this);
customDialog.setTitle("提示").setMessage("确认删除?")
.setCancel("取消", new CustomDialog.IOnCancelListener() {
@Override
public void onCancel(CustomDialog dialog) {
Toast.makeText(CustomDialogActivity.this, "取消", Toast.LENGTH_SHORT).show();
customDialog.dismiss(); }
})
.setConfirm("确认", new CustomDialog.IOnConfirmlListener() {
@Override
public void onConfirm(CustomDialog dialog) {
Toast.makeText(CustomDialogActivity.this, "确定", Toast.LENGTH_SHORT).show();
customDialog.dismiss();
}
}).show();
}
});
}
}
运行截图:


Android学习07的更多相关文章
- Android 学习资源
下面这些资源对Android开发来说是很有帮助的! 最常用的: Android开发官方网站:http://developer.android.com/index.html 这个网站应该是Android ...
- Android学习资料收集
1.Android 学习之路 http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/
- Android学习——windows下搭建NDK_r9环境
1. NDK(Native Development Kit) 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP功能开发的工具,通过这个 ...
- Android 学习之路
转载:http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/ 这篇博客背后的故事 一路走来很不容易,刚好知乎上被人邀请回 ...
- 10、android学习资源整理
1.github上整理好的开源工程 https://github.com/Trinea/android-open-project 2.最流行的android组件大全 http://colobu.com ...
- android学习系列:jercy——AI3 的博客
[android学习之十七]——特色功能2:桌面组件(快捷方式,实时文件夹) 二.桌面组件 1.快捷方式 Android手机上得快捷方式的意思可以以我们实际PC机器上程序的快捷方式来理解.而andro ...
- Android 学习之路和App开发框架
学习之路: 1. http://www.stormzhang.com/android/2014/07/07/learn-android-from-rookie/ 框架: 2. https://gith ...
- Android学习路线总结,绝对干货
title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...
- Android学习——第一个NDK程序
在前面的学习中,我们已经讲解了关于NDK编程的环境搭建流程,简单的使用我们也通过官网本身自带的例子进行说明了.可是相信大家一定还存在这么的一个疑惑:“如果我要自己利用NDK编写一个Android应用, ...
随机推荐
- C语言循环语句工程用法
-循环语句分析 循环语句的基本工作方式 - 通过条件表达式判断是否执行循环体 - 条件表达式循环if语句表达式的原则 do.while.for的区别 - do语句先执行后判断,循环体至少循环一次 - ...
- Mapper-元素和属性
Mapper.xml文件内部的元素和属性 parameterType(输入类型) § 传递简单类型 § 使用#{}占位符,或者${}进行sql拼接, #{}括号中的值可以任意, ${}括号 ...
- 最大/最小de K个数/第K个数
题目 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 思路 堆排序 收获 用优先队列实现最大最小堆 注意下列代码中优先队列 ...
- 初识Docker:BusyBox容器后台运行失败
1.问题描述:我在进行Docker网络实验时,使用docker run -d busybox命令时,busybox无法保持后台长期运行. ============================ ...
- JavaWeb项目忘记添加依赖
有的时候我们建项目的时候忘记添加项目的依赖了,这里示范一个提示错误,就是 The superclass "javax.servlet.http.HttpServlet" was n ...
- .NET知识梳理——2.反射
1. 反射 1.1 DLL-IL-Metadata-反射 DLL:程序集,包含IL 和Metadada IL:面向对象中间语言(不太好阅读) Metadata描述了dll.exe中的各种 ...
- 我的reshape观
reshape(1,2)把结果分成1块,每一块2个元素 reshape(2,1)把结果分成2块,每一块1个元素 reshape(-1,1)把结果分成任意块,每一块1个元素 reshape(1,-1)把 ...
- es8中对string补白的方式
//允许将空字符串或其他字符串添加到原始字符串的开头或结尾for(let i = 1; i < 32; i++) { if(i < 10) { console.log(`0{i}`) }e ...
- Python爬虫连载7-cookie的保存与读取、SSL讲解
一.cookie的保存与读取 1.cookie的保存-FileCookie.Jar from urllib import request,parse from http import cookieja ...
- 【python】anaconda中打开IDLE(python 自带编辑器)
最近要参加蓝桥杯了,发现 python 的编辑器是使用 python 自带的 IDLE,电脑上只用 Anaconda,就来找一下 打开 .\Anaconda3\Scripts\idel.exe 打开 ...