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应用, ...
随机推荐
- eclipse的安装和环境配置
一,eclipse下载 地址:https://www.eclipse.org/downloads/ 一般浏览器都有翻译功能 二.有32位和64位的版本根据自己的需求下载,选下载的选下载量最多的下载. ...
- 原来window 也可以使用pthreads
https://blog.csdn.net/clever101/article/details/101029850
- [一本通学习笔记] 最近公共祖先LCA
本节内容过于暴力没什么好说的.借着这个专题改掉写倍增的陋习,虽然写链剖代码长了点不过常数小还是很香. 10130. 「一本通 4.4 例 1」点的距离 #include <bits/stdc++ ...
- python3练习100题——018
原题链接:http://www.runoob.com/python/python-exercise-example18.html 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个 ...
- 月薪20k的web前端开发程序员,他们都会的这6招
web前端工程师是近几年的新兴职业,也是目前火爆而且高薪的职业.不同的公司也有不同的叫法,比如:网页界面开发,网站设计等,要学好web前端开发,需要掌握什么方法与技巧? 一.div和table 这个是 ...
- STL初探
关于STL的一些东西 感言: 学C++不学STL函数库的人可能都是... 有点问题 头文件<algorithm>的一些东西 sort,快排: 这是个初学者必需掌握的东西,及其好用,因为方( ...
- jquery使用ajax实现实时刷新,轮询
来自:https://blog.csdn.net/qq_25406669/article/details/78343082 var isLoaded = false; function reqs() ...
- codeforce F - Three Paths on a Tree
F. Three Paths on a Tree time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- SpringMVC项目使用elastic search搜索
项目需要,引入了elastic search(后续简称es),后面将介绍本地对es的安装,使用以及java连接es查询的整个过程. 1.es索引字段建立与修改,以curl新增一个索引字段示例 curl ...
- 问题 B: 奇怪的电梯
问题 B: 奇怪的电梯 时间限制: 1 Sec 内存限制: 128 MB[命题人:admin] 题目描述 大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0& ...