自定义AlertDialog(圆角+退出动画+自定义布局)

2024-08-31 23:58

本文主要是介绍自定义AlertDialog(圆角+退出动画+自定义布局),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

图片省略了不上传了。。。

首先看activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="点击我,弹出AlertDialog" /></RelativeLayout>
MainActivity.java

package com.godinsec.alertdialog;import com.godinsec.alertdialog.ShowDeleDialog.onBtnClickListener;import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {new ShowDeleDialog(MainActivity.this, new onBtnClickListener(){@Overridepublic void onSure() {Toast.makeText(MainActivity.this, "进行确认操作", 0).show();}@Overridepublic void onExit() {Toast.makeText(MainActivity.this, "进行取消操作", 0).show();}}, "确定", "取消", "提示", "AlertDialog的内容").show();}});}}

接下来看自定义alertDialog

dele_allcontacts_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><RelativeLayoutandroid:id="@+id/re_tip_dialog"android:layout_width="300dp"android:layout_height="120dp"android:layout_centerInParent="true"android:background="@drawable/rounded_search_text"><TextViewandroid:id="@+id/appTitle"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="标题"/><TextViewandroid:id="@+id/tips"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/appTitle"android:layout_alignLeft="@id/appTitle"android:text="AlertDialog的内容"android:layout_marginTop="20dp"/><TextViewandroid:id="@+id/sure"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="确认"android:layout_below="@id/tips"android:layout_marginTop="20dp" /><TextViewandroid:id="@+id/cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@id/sure"android:text="取消"android:layout_marginRight="30dp"android:layout_toLeftOf="@id/sure" /></RelativeLayout></RelativeLayout>
rounded_search_text.xml(圆角)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" ><solid android:color="#FF7F24" /><cornersandroid:bottomLeftRadius="4dp"android:bottomRightRadius="4dp"android:topLeftRadius="4dp"android:topRightRadius="4dp" /></shape>
ShowDeleDialog.java

package com.godinsec.alertdialog;import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;public class ShowDeleDialog extends Dialog implementsandroid.view.View.OnClickListener {private onBtnClickListener onBtnClickListener;private Context context;private String sure;private String cancle;private String appTitle;private String tips;private TextView tv_dele_sure;private TextView tv_dele_cancle;private TextView tv_appTitle;private TextView tv_tips;private RelativeLayout re_tip_dialog;public ShowDeleDialog(Context context,onBtnClickListener onBtnClickListener, String sure, String cancle,String appTitle, String tips) {super(context);this.onBtnClickListener = onBtnClickListener;this.context = context;this.appTitle = appTitle;this.tips = tips;this.sure = sure;this.cancle = cancle;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 去除默认的头部标题requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.dele_allcontacts_dialog);tv_dele_sure = (TextView) findViewById(R.id.sure);tv_dele_cancle = (TextView) findViewById(R.id.cancel);tv_appTitle = (TextView) findViewById(R.id.appTitle);tv_tips = (TextView) findViewById(R.id.tips);re_tip_dialog = (RelativeLayout) findViewById(R.id.re_tip_dialog);tv_dele_cancle.setOnClickListener(this);tv_dele_sure.setOnClickListener(this);//进入动画是伪代码
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.0f,  
0.0f, 1.0f, Animation.RELATIVE_TO_SELF,  
0.5f, Animation.RELATIVE_TO_SELF,  
0.5f);  
scaleAnimation.setDuration(200);  
re_tip_dialog.startAnimation(scaleAnimation);
Window window = this.getWindow();WindowManager.LayoutParams params = this.getWindow().getAttributes();// 去除四角黑色背景window.setBackgroundDrawable(new BitmapDrawable());// 设置周围的暗色系数params.dimAmount = 0.5f;window.setAttributes(params);// 为各个textview赋值tv_dele_sure.setText(sure);tv_dele_cancle.setText(cancle);tv_appTitle.setText(appTitle);tv_tips.setText(tips);}public interface onBtnClickListener {public void onSure();public void onExit();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.cancel:onBtnClickListener.onExit();alertDialogExitAnim();break;case R.id.sure:onBtnClickListener.onSure();alertDialogExitAnim();break;default:break;}}private void alertDialogExitAnim() {ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f,1.0f, 0.0f, Animation.ABSOLUTE,re_tip_dialog.getWidth() / 2, Animation.ABSOLUTE,re_tip_dialog.getHeight() / 2);scaleAnimation.setDuration(1000);re_tip_dialog.startAnimation(scaleAnimation);scaleAnimation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {ShowDeleDialog.this.dismiss();}});}}





这篇关于自定义AlertDialog(圆角+退出动画+自定义布局)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125366

相关文章

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

Qt QWidget实现图片旋转动画

《QtQWidget实现图片旋转动画》这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、效果展示二、源码分享本例程通过QGraphicsView实现svg格式图片旋转。.hpjavascript