android 开发 实现一个自定义布局的AlertDialog对话框

AlertDialog的简单使用,五个实例 我分别用了5个按钮来演示这五个例子的效果,我先来讲解一下这五个效果分别是什么吧: 一个警告对话框上面有两个按钮 一个警告对话框上面有两个选项 一个警告对话框上面有两个单选按钮 一个警告对话框上面有三个多选框和两个按钮 一个自定义的警告对话框 在注释中我把警告对话框简写为警告框了,下面来看下完整的代码: AlertDialogActivity中的代码: import android.conten... 阅读详情

   对话框有很多实现方法,最常见的是在一个点击事件中代码直接写出对话框。如下:

package com.example.lenovo.mydemo2;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button)findViewById(R.id.button1);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                //对话框标题设置
                dialog.setTitle("这个是标题");
                //对话框内容设置
                dialog.setMessage("这个是内容");
                //对话框设置不可以用Back键退出
                dialog.setCancelable(false);
               // dialog.clone()
                /*
                三种Button
                Positive Button  正面按键
                Negative Button  负面按键
                Neutral Button   中性按键
                 */
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //关闭对话框
                        dialog.dismiss();
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //关闭对话框
                        dialog.dismiss();

                    }
                });
                dialog.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
       :             }
                });
                //不要忘记了给对话框添加显示。
                dialog.show();
            }
        });
    }
}

运行效果:

以上就是直接在代码上实现对话框,但是这样实现对话框有一个缺点,那就是真心不好看,在实际项目中对话框与项目的主题不配套,所以我们就需要自定义实现对话框布局了:

1.我们首先需要写一个在对话框后点击效果的背景布局图片:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="false">
        <color android:color="@color/colorWhite"/>
    </item>
    <item
        android:state_pressed="true">
        <color android:color="@color/colorWhiteGray"/>
    </item>
</selector>

2.然后需要写一个用于对话框的布局:

<?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="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_woman_default"/>
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="选择添加方式"
             android:textSize="@dimen/BigTextSize"
             android:textColor="@color/colorBlue"
             android:layout_gravity="center"
             android:layout_marginLeft="10dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/colorBlue"
        android:layout_margin="10dp">
    </LinearLayout>
     <LinearLayout
         android:id="@+id/PersonalDataModification_Dialog_CameraButton"
         android:orientation="horizontal"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="@drawable/button_background_white_change_gray">
         <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:src="@mipmap/ic_camera"/>
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="拍摄照片"
             android:textSize="@dimen/BigTextSize"
             android:textColor="@color/colorBlue"
             android:layout_gravity="center"
             android:layout_marginLeft="20dp"
             />
     </LinearLayout>
        <LinearLayout
            android:id="@+id/PersonalDataModification_Dialog_GalleryButton"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_background_white_change_gray">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_gallery"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="在相册中选择"
                android:textColor="@color/colorBlue"
                android:textSize="@dimen/BigTextSize"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

效果图:

3.下面是实现对话框的Java代码部分:

public void dialogueBox(){
        AlertDialog.Builder dialog = new AlertDialog.Builder(PersonalDataModification.this);
        View view = LayoutInflater.from(this.getBaseContext()).inflate(R.layout.dialog_layout,null,false);
        dialog.setView(view);
        mDialog_CameraButton = (LinearLayout)view.findViewById(R.id.PersonalDataModification_Dialog_CameraButton);
        mDialog_GalleryButton = (LinearLayout)view.findViewById(R.id.PersonalDataModification_Dialog_GalleryButton);
        mDialog_CameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(PersonalDataModification.this,"点了进入相机",Toast.LENGTH_SHORT).show();
            }
        });
        mDialog_GalleryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(PersonalDataModification.this,"点了进入相册",Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();
    }

将上面的方法添加到一个点击事件中,就可以实现对话框了。下面我们来看看运行效果:

Rust+Tauri 调用系统原生能力完全指南:桌面 / Android / iOS / HarmonyOS 本文深入探讨了Tauri框架如何通过Rust内核与系统WebView实现跨平台开发,打通H5与系统能力的交互链路。文章从Tauri的四层架构(前端、WebView、Rust内核、原生层)入手,详细解析了JS与Rust的IPC通信原理,并横向对比了桌面、Android、iOS、HarmonyOS四端的实现差异。核心结论包括:Tauri采用Rust作为唯一系统能力持有者,通过统一IPC通道连接前端与底层;移动端官方支持Android/iOS,鸿蒙需社区扩展;开发者可保持同一套H5代码和Rust业务逻辑,仅通过条 阅读详情

相关推荐

Android Performance】进程性能配置三板斧——task_profiles、IO优先级与sched_boost调度提升实战

本文系统讲解Android系统中为关键进程"提性能"的三板斧配置手段,以update_engine(OTA升级服务)为贯穿示例。文章先建立全局认知:进程性能受三层资源制约——CPU调度、IO优先级、调度器boost行为,对应三种配置手段各司其职;随后逐一拆解:task_profiles机制(在init rc文件中用一行task_profiles指令为service挂载预定义性能配置组。

weixin_44081096的博客 449

Android自定义AlertDialog组件

本人是Android开发菜鸟,最近总结了两种自定义AlertDialog对话框的方法,文章或有不足之处,还望各位大神多给高见,勿喷。 一下是我自定义的Dialog截图,直接上图(我自己都觉的很丑,不过重要的是方法) 方法一: 1.首先要定义自己的AlertDialog对话框的资源布局文件mydialog.xml 2.为了效使效果更好我们还需要为上述mydialog资源文件中所包含的

千年孤寂 641

自定义AlertDialog对话框以优化Android耗时任务提示

本文还有配套的精品资源,点击获取 简介:在Android应用开发中,为保证良好的用户交互体验,对耗时任务(如网络请求、数据库操作、文件加载等)进行良好的状态提示至关重要。本文详细介绍了如何通过自定义AlertDialog对话框实现耗时任务的加载提示,包括创建自定义对话框布局实现代码逻辑,以及如何通过事件监听器处理用户的交互行为(如点击取消按钮)。同时强调了在主线程之外执...

weixin_30533301的博客 1104

Android 自定义AlertDialog提交表单

Android在前端的设计中,有些时候我们需要提交一些数据或表单的时候,由于数据量不是很大,我们没有必要创建一个全新的Activity来提交很少的数据,这样会显得很单调,也浪费资源,其实不只是Android中这样,网页设计也会大量利用弹窗式对话框来提交一些数据。这个时候我们就可以使用自定义AlertDialog。 1、创建AlertDialog显示所需要的布局文件。这里是一个修改密码的布局文件

PlanZ的博客 1741

android仿ios自定义弹框,Android仿IOS自定义AlertDialog提示框

Android仿IOS自定义AlertDialog提示框发布时间:2020-09-07 15:13:23来源:脚本之家阅读:104作者:蒽香之气本文介绍的仿IOS对话框实现,先来看一下效果图具体代码如下:public class AlertDialog {private Context context;private Dialog dialog;private LinearLayout lLay...

weixin_42292183的博客 459

Android 常见对话框的简单使用(提示信息对话框、单选多选对话框自定义对话框)(1)

自定义对话框布局

2401_84123265的博客 980

Android学习之AlertDialog

为了创建AlertDialog,需要使用Builder类,创建AlertDialog.Builder类的对象实例,再通过show()方法显示对话框。 在编程的过程中发现SetMessage()方法和setSingleChoiceItems()等方法是互斥的,这个需要注意。 废话不多说,下面列出了8种AlertDialog对话框: 1 主界面布局 <LinearLayout xmlns:an

cjllife的专栏 1068

Android ANR实战排查:主线程SystemClock.sleep导致页面启动偶现无响应

🔍 ANR问题全链路解决方案 摘要:本文系统讲解Android ANR问题的全链路解决方案。首先指出单纯依赖Logcat无法定位根因的局限,详细介绍了两种ANR日志抓取方法:adb pull /data/anr/(适合开发板)和dumpsys dropbox(适合商用手机)。通过真实案例堆栈分析,揭示了主线程调用第三方SDK同步方法导致SystemClock.sleep()阻塞的根因,并强调该问题偶现性特征。最后提出分层解决方案:应用层采用协程异步初始化,或SDK层改造为异步回调模式,并列出了五大开发避坑

知道的越多,不知道的越多。 382

Android16 默认第三方Launcher为主桌面

【代码】Android16 默认第三方Launcher为主桌面。

qq_19447521的博客 37

Android 新闻资讯客户端实战:从零搭建一个完整的新闻 App

在移动互联网时代,新闻资讯类 App 是最常见的应用场景之一。OkHttp 网络请求封装(GET / POST / PUT / DELETE / 文件上传)ViewPager + TabLayout + Fragment 的经典 Tab 分页模式Gson JSON 数据解析与 JavaBean 映射Glide 图片加载框架的使用SQLite 本地数据库持久化SharedPreferences 登录态管理。

mmsx990的博客 269

手机远程控制电脑全平台横测:iOS安卓鸿蒙谁更强

一、手机控电脑,到底有多刚需?你可能也经历过这些场景:下班刚到家,老板打电话说有个紧急数据需要从公司电脑里拉出来周末在外面逛街,突然想起家里电脑上的渲染任务还没点"开始"出差在高铁上,只能用手机远程登上工作电脑改一个配置父母打电话说电脑出了奇怪的问题,你人在千里之外干着急这些时刻,不再是"可选项",而是"救命稻草"。但市面上的远程控制 App 一个一个能吹——“高清流畅”、“低延迟”、“一键连接”。真用起来呢?画质糊成马赛克、触控操作反人类、键盘输入比手写还慢……到底哪一款才是真正好用的。

2401_89864609的博客 311

一个安卓程序

本文简要介绍如何使用Android Studio创建第一个安卓HelloWorld程序:1)新建EmptyViewActivity项目并命名;2)定位到layout目录下的activity_main.xml文件;3)配置虚拟设备(需提前下载系统镜像);4)运行程序。通过这四步即可完成基础安卓应用的创建与测试。全文以操作流程为导向,适合初学者快速入门。(98字)

UE星空 178

Android录屏内部音频录制完全指南:原理、限制与方案选型

本文解析了安卓录屏无声问题的根源和解决方案。核心要点包括: 1)安卓10+才支持内部音频捕获,低版本系统无法实现; 2)音频来源分内部音频、麦克风和静音三种模式; 3)部分应用会禁止音频捕获进行版权保护; 4)给出参数设置建议和方案选型(系统自带、第三方工具、电脑投屏),推荐快录屏APP; 5)提供6步排查清单。文章指出多数情况是系统限制或配置问题,帮助用户针对性解决录屏无声问题。

371

保姆级智能座舱测试入门:从 Android Automotive OS 到 SOA 服务的 7 层验证

本文介绍了智能座舱的7层测试体系,从底层OS到顶层用户体验全覆盖验证。随着智能座舱复杂度提升,传统手工测试已无法满足需求。7个层级包括:OS内核层(L1)、硬件抽象层(L2)、框架层(L3)、应用层(L4)、SOA服务层(L5)、HMI/UX层(L6)和性能层(L7)。每层详述了测试目标、核心测试项及工具链,如LTP、VTS、CTS、CANoe等。

XiuSLjie的博客 251

【阅读源码-Android】动画之Choreographer-2

注册 存储 调度 执行 回收│ │ │ │ │▼ ▼ ▼ ▼ ▼│ │ │ ││ │ → 直接请求 │ DueLocked │ ├─ token=null│ │ │ │ └─ 放回 mCallbackPool│ Locked │▼ ▼(按 dueTime 排序)单 Vsync 请求确保一帧只请求一次 vsync,避免重复 IPC对象池复用通过复用,避免高频分配导致的 GC 压力异步消息:所有帧相关消息都设为 asynchronous,避免被同步屏障阻挡两级 Jitter 修正。

凉水的博客 353

不装 Android Studio,也能把 Vue / React 一键打成 APK:开源工具 lw.Web2Android

lw.Web2Android是一款轻量级开源工具,专注于将Web项目快速打包为Android APK,无需配置复杂的Android开发环境。它支持HTML、Vue、React等静态项目,采用预编译Runtime技术简化构建流程,自动管理APK签名,适合企业内部系统、展示类App等场景。与Capacitor等混合开发框架不同,它保持功能精简,专注于解决"已有Web项目转APK"的核心需求。项目目前处于早期阶段,强调稳定性和可靠性而非功能堆砌。

.NET 人工智能实践 322
上一篇: android 开发 实现RecyclerView的列表单选功能
下一篇: android 开发 对话框Dialog详解
Thy拾
博客等级 码龄8年 26粉丝 293原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值