Android的 AlertDialog自定义布局与常用布局用法(弹窗)

article/2025/10/31 15:27:09

1.直接上效果图,看看是不是你们想要的效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.主活动MainActivity2的代码如下


import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;import java.util.Arrays;
import java.util.List;public class MainActivity2 extends AppCompatActivity {private Button button = null;private Button button2 = null;@SuppressLint("MissingInflatedId")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);button = (Button) findViewById(R.id.button);button.setOnClickListener( new View.OnClickListener() {@Overridepublic void onClick(View v){dialogShow1();}});button2 = (Button) findViewById(R.id.button2);button2.setOnClickListener( new View.OnClickListener() {@Overridepublic void onClick(View v){dialogShow2();}});}private void dialogShow1() {AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);builder.setTitle("温馨提示");builder.setIcon(R.drawable.ic_launcher_background);builder.setMessage("原理是基本");builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {Toast.makeText(MainActivity2.this, "no", Toast.LENGTH_LONG).show();}});builder.setPositiveButton("立即更新",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {Toast.makeText(MainActivity2.this, "ok", Toast.LENGTH_LONG).show();}});Dialog dialog = builder.create();dialog.show();}/*** 自定义布局* setView()只会覆盖AlertDialog的Title与Button之间的那部分,而setContentView()则会覆盖全部,* setContentView()必须放在show()的后面*/private void dialogShow2() {AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);LayoutInflater inflater = LayoutInflater.from(MainActivity2.this);View v = inflater.inflate(R.layout.update_manage_dialog, null);TextView content = (TextView) v.findViewById(R.id.dialog_content);Button btn_sure = (Button) v.findViewById(R.id.dialog_btn_sure);Button btn_cancel = (Button) v.findViewById(R.id.dialog_btn_cancel);//builer.setView(v);//这里如果使用builer.setView(v),自定义布局只会覆盖title和button之间的那部分final Dialog dialog = builder.create();dialog.show();dialog.getWindow().setContentView(v);//自定义布局应该在这里添加,要在dialog.show()的后面//dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置content.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity2.this, "用户点击的是:"+content.getText(), Toast.LENGTH_LONG).show();}});btn_sure.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();Toast.makeText(MainActivity2.this, "ok", Toast.LENGTH_LONG).show();}});btn_cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {dialog.dismiss();Toast.makeText(MainActivity2.this, "no", Toast.LENGTH_LONG).show();}});}}

3.activity_main2的布局文件内容如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.myapplication001.MainActivity2"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#03A9F4"android:text="弹出dialog"android:textColor="#F3EEEE" /><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:background="#03A9F4"android:text="弹出自定义布局dialog"android:textColor="#F4F2F2" /></LinearLayout></RelativeLayout>

4.自定义弹出窗的布局:update_manage_dialog布局文件内容如下

<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"android:background="#00FFFFFF" ><RelativeLayoutandroid:layout_width="250dp"android:layout_height="250dp"android:layout_centerInParent="true"android:background="@drawable/update_bg" ><TextViewandroid:id="@+id/dialog_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:gravity="center"android:text="温馨提示"android:textSize="18sp" /><TextViewandroid:id="@+id/dialog_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/dialog_title"android:layout_marginTop="10dp"android:layout_marginLeft="30dp"android:layout_marginRight="30dp"android:text="原理是基本\n实践出真知"android:textSize="14sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:orientation="horizontal" ><Buttonandroid:id="@+id/dialog_btn_cancel"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@null"android:text="取消"android:textColor="#AAAAAA"android:textSize="14sp" /><Buttonandroid:id="@+id/dialog_btn_sure"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@null"android:text="立即更新"android:textSize="14sp" /></LinearLayout></RelativeLayout></RelativeLayout>

5.update_bg放在drawable里面,代码如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><!-- android:radius 弧形的半径 --><corners android:radius="30dp" /><!-- 填充的颜色 --><solid android:color="@android:color/white" /></shape>

http://chatgpt.dhexx.cn/article/fc6gqIPT.shtml

相关文章

AlertDialog详解

对话框介绍与示例 对话框在程序中不是必备的&#xff0c;但是用好对话框能对我们编写的应用增色不少。采用对话框可以大大增加应用的友好性。比较常用的背景是&#xff1a;用户登陆、网络正在下载、下载成功或者失败的提示&#xff0c;还有&#xff0c;比如&#xff1a;短信来了…

Android的AlertDialog详解

转自&#xff1a;http://www.2cto.com/kf/201205/131876.html 参考文章&#xff1a;http://www.cnblogs.com/jiezzy/archive/2012/09/20/2694917.html http://blog.csdn.net/lizzy115/article/details/6924416 AlertDialog的构造方法全部是Protected的&#xff0c;所以不能直接…

AndroidStudio中AlertDialog的四种用法

目录 1.默认样式 2.单选弹出框 3.多选弹出框 4.自定义弹出框 补充&#xff01;&#xff01; 1.默认样式 android.support.v7.app.AlertDialog.Builder builder new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this); builder.setTitle("请回…

Android之AlertDialog的基础使用

坦白说&#xff0c;AlertDialog我在工作中用得并不多&#xff0c;因为AlertDialog的样式比较固定和呆板&#xff0c;为了和App的整体设计匹配&#xff0c;一般都是使用自定义的Dialog&#xff0c;只有在要求不高时用一下。但是作为Android的基础控件之一&#xff0c;掌握它是十…

安卓AlertDialog弹窗

Android在开发中经常会遇到有弹框的需求。 经常使用的有Dialog 弹框&#xff0c;Window弹框&#xff08;任意位置弹出除了外观样式和显示的位置的区别之外&#xff0c;他们之间最本质的区别是&#xff1a; dialog是非阻塞式对话框&#xff0c;popupwindow是阻塞式对话框。也就…

AlertDialog对话框的简单使用

目录 一、对话框的创建 二、单选的对话框 三、多选的对话框 一、对话框的创建 一般的对话框分为标题、内容、按钮三大部分。 常见的方法&#xff1a; 方法功能setTitle()设置对话框的标题setIcon()设置对话框的图标setMessage()设置对话框的提示信息setPositiveButton()设…

alertDialog使用详解

1、设置标题、内容、图标 2、设置按钮 3、使用列表、单选和多选。适配 4、设定弹窗大小 5、自定义view 6、设置点击周边灰色区域弹窗不消失 7、自定义view圆角消除周边白块 1、设置标题、内容、图标 AlertDialog alertDialog new AlertDialog.Builder(this) .setTit…

AlertDialog6种使用方法

AlertDialog 1.AlertDialog的6种创建模式 1.1setMessage 1&#xff09;Java代码 //1.创建构造器AlertDialog.Builder buildernew AlertDialog.Builder(this);//2.设置参数builder.setTitle("弹窗提示").setIcon(R.mipmap.boy).setMessage("选择你的性别&#xf…

Android的AlertDialog详解(7种方式)

需要注意的两点&#xff1a; 1. 在setIcon时&#xff0c;需要使用setTitle方法&#xff0c;否则icon不会显示 2.如果同时调用setMessage 和 setItems(或者setSingleChoiceItems setMultiChoiceItems)函数会导致dialog没有显示内容 AlertDialog的构造方法全部是Protected的&am…

AlertDialog(对话框)详解

AlertDialog可以在当前的界面上显示一个对话框&#xff0c;这个对话框是置顶于所有界面元素之上的&#xff0c;能够屏蔽掉其他控件的交互能力&#xff0c;因此AlertDialog一般是用于提示一些非常重要的内容或者警告信息。 1.创建AlertDialog 首先&#xff0c;我们来了解一下Al…

andoid小游戏开发

apk下载&#xff1a;apk下载&#xff1a;http://download.csdn.net/detail/xiangqiao123/3805861 这是去年用android写的一个小游戏&#xff0c; 我感觉不错&#xff0c;是从事android小游戏开发入门不错的案例&#xff0c; 今天就把它拿出来和大家共享一下。 程序截图&a…

android 2D 游戏的开发的方法

最近学习了android 2D 应用的开发&#xff0c;拿来和大家分享一下&#xff0c;学习2D 开发前我们先了解一下SurfaceView的使用以及贴图技术的使用&#xff0c;最后呢&#xff0c;是一个简单的2的游戏的实现。 1.SurfaceView的一些用法 提供了一个专门的绘图渲染的图形嵌入在一个…

Android手机游戏开发入门教程

Android手机游戏开发入门教程 视频欣赏地址 来自 “ ITPUB博客 ” &#xff0c;链接&#xff1a;http://blog.itpub.net/29597077/viewspace-1139520/&#xff0c;如需转载&#xff0c;请注明出处&#xff0c;否则将追究法律责任。 转载于:http://blog.itpub.net/29597077/vie…

Android 游戏开发速递

作者 / Greg Hartrell, Head of Product Management, Games on Android & Google Play 在今年 3 月举行的 Google 游戏开发者峰会上&#xff0c;我们分享了 Google 为帮助游戏开发者而持续投入研发的数种新工具和服务。这些新工具和服务能够帮助游戏开发者更轻松地查看其 A…

游戏开发相关

游戏开发—图形图像篇 游戏开发--开篇  记得我第一次玩的PC game 是KKND(绝地风暴)&#xff0c;当时的游戏平台是DOS&#xff0c;我只是觉得很好玩&#xff0c;经常和几个小学同学一起厮杀到12点。可是现在回忆起来&#xff0c;KKND无论是从智能设计还是在游戏画面与操作上都…

android小游戏制作基础,View实现游戏布局和方法

在使用android的朋友们&#xff0c;相信大家对android的游戏不陌生吧&#xff0c;像愤怒的小鸟&#xff0c;植物大战僵尸等等优秀的游戏&#xff0c;给我们带来了很好的用户体验 下面我来教大家一点android游戏开发的一点基础&#xff0c;大家可以参照这个方法框架来设计一些像…

android游戏开发的架构

&#xfeff;&#xfeff; 在编写游戏代码之前&#xff0c;必须要仔细地理顺思路&#xff0c;清晰地构建出整个游戏的框架。有的开发者经常抱怨说&#xff0c;游戏开发到最后总是千头万绪&#xff0c;一旦出现bug就不知道该如何修改&#xff0c;身心疲惫甚至是痛不欲生。其实不…

用Unity3d开发Android游戏

Unity3d是个强大的游戏引擎&#xff0c;可以很轻松的将游戏发布到Android平台上&#xff0c;今天我就来讲讲如何用Android来开发Android游戏。 首先我们要下载Android SDK&#xff0c;可以在http://developer.android.com/sdk/index.html这里下载到&#xff0c;运行installer安…

Android游戏开发的入门实例

在Android系统上开发游戏是Android开发学习者所向往的&#xff0c;有成就感也有乐趣&#xff0c;还能取得经济上的报酬。那怎样开发Android游戏呢&#xff1f;下面介绍一个简单的入门实例。 一、创建新工程   首先&#xff0c;我们在Eclipse中新建一个名为Movement的工程&…

如何开发手机游戏?

当今社会&#xff0c;手机游戏无非是当下在旅途中打发时间的最便捷的方式。有关数据显示&#xff0c;62% 的智能手机用户在购买智能手机后的一周内安装了游戏。无论你是参加聚会还是度假&#xff0c;或者周末宅在家里&#xff0c;手机都是玩游戏最方便的设备。这也正是手机游戏…