iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android Dialog对话框详解
  • 475
分享到

Android Dialog对话框详解

dialogAndroid 2022-06-06 08:06:19 475人浏览 独家记忆
摘要

废话不多说了,直接给大家贴代码了。 布局文件xml: <LinearLayout xmlns:Android="Http://schemas.android.com/a

废话不多说了,直接给大家贴代码了。

布局文件xml:


<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DialogActivity" >
<Button
android:id="@+id/plainDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通Dialog" />
<Button
android:id="@+id/plainDialogEvent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog按钮事件集中处理" />
<Button
android:id="@+id/inputDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入框" />
<Button
android:id="@+id/listDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表对话框" />
<Button
android:id="@+id/radioDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框" />
<Button
android:id="@+id/checkboxDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框" />
<Button
android:id="@+id/diyDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义布局对话框" />
</LinearLayout> 

Activity文件:

普通的dialog:


private void plainDialogDemo() {
Button plainBtn = (Button) findViewById(R.id.plainDialog);
plainBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(DialogActivity.this)
.setTitle("删除")
.setMessage("确定删除指定数据")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(),
"确定了", Toast.LENGTH_SHORT)
.show();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
}).setCancelable(false).show();
}
});
}

效果如下:

 

输入文本框的dialog:


private void inputDialog() {
Button inputBtn = (Button) findViewById(R.id.inputDialog);
inputBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final EditText et = new EditText(DialogActivity.this);
new AlertDialog.Builder(DialogActivity.this)
.setTitle("请输入数字")
.setView(et)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
et.getText(),
Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", null)
.setCancelable(false).show();
}
});
}

效果如下:


列表dialog:


private void listDialogDemo() {
Button listBtn = (Button) findViewById(R.id.listDialog);
listBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[] names = { "C罗", "J罗", "H罗" };
new AlertDialog.Builder(DialogActivity.this).setTitle("列表对话框")
.setItems(names, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(DialogActivity.this,
names[which], Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("取消", null).show();
}
});
} 

效果如下:


单选dialog:


private void radioDialogDemo() {
Button radioButton = (Button) findViewById(R.id.radioDialog);
radioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[] names = { "C罗", "J罗", "H罗" };
new AlertDialog.Builder(DialogActivity.this)
.setTitle("列表对话框")
.setSingleChoiceItems(names, ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
selecteName = names[which];
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(DialogActivity.this,
selecteName, Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("取消", null).show();
}
});
}

效果如下:


多选dialog:


private void checkDialogDemo() {
Button checkBtn = (Button) findViewById(R.id.checkboxDialog);
checkBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[] names = { "C罗", "J罗", "H罗" };
final boolean[] selected = new boolean[] { true, false, true };
new AlertDialog.Builder(DialogActivity.this)
.setMultiChoiceItems(
names,
selected,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
StringBuilder sb = new StringBuilder(
"你选择了:");
for (int i = ; i < names.length; i++) {
if (selected[i]) {
sb.append(names[i]);
}
}
Toast.makeText(DialogActivity.this,
sb.toString(), ).show();
}
}).setNegativeButton("取消", null).show();
}
});
}

效果如下:


自定义dialog:


private void customDialogDemo() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
window.setContentView(R.layout.diylayout);
ImageButton ok = (ImageButton) window.findViewById(R.id.btnok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "关闭了",
Toast.LENGTH_SHORT).show();
dlg.dismiss();
}
});
}

自定义布局:


<?xml version="." encoding="utf-"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/dialogimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/dialog_bg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/dialogimg"
android:layout_alignTop="@id/dialogimg"
android:layout_marginLeft="dp"
android:layout_marginTop="dp"
android:text="自定义的dialog" />
<ImageButton
android:id="@+id/btnok"
android:layout_width="dp"
android:layout_height="dp"
android:layout_alignRight="@id/dialogimg"
android:layout_alignTop="@id/dialogimg"
android:layout_marginRight="dp"
android:layout_marginTop="dp"
android:background="@drawable/close_dialog" />
</RelativeLayout> 

效果如:

有关Android Dialog对话框详解小编就给大家介绍这么多,希望对大家有所帮助!

您可能感兴趣的文章:Android使用AlertDialog实现的信息列表单选、多选对话框功能Android使用自定义alertdialog实现确认退出按钮Android实现点击AlertDialog上按钮时不关闭对话框的方法Android中自定义对话框(Dialog)的实例代码8种android 对话框(Dialog)使用方法详解Android UI设计系列之自定义Dialog实现各种风格的对话框效果(7)属于自己的Android对话框(Dialog)自定义集合Android中制作自定义dialog对话框的实例分享Android实现自定义圆角对话框Dialog的示例代码Android开发之利用Activity实现Dialog对话框Android编程实现带有单选按钮和复选按钮的dialog功能示例


--结束END--

本文标题: Android Dialog对话框详解

本文链接: https://www.lsjlt.com/news/25357.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 详解Android Dialog对话框的五种形式
    Android中的对话框形式大致可分为五种:分别是一般对话框形式,列表对话框形式,单选按钮对话框,多选按钮对话框,自定义对话框。在实际开发中,用系统的对话框会很少,因为太丑了,美工不愿意,多是使用自定义对话框。当然学会系统的,自定义就简单了...
    99+
    2023-05-31
    android dialog 对话框
  • Android对话框AlertDialog详解
    目录1.创建AlertDialog1.1 布局文件代码如下:1.2 MainActivity的主要代码如下所示:2.普通提示对话框3.普通列表对话框4.单选对话框5.复选对话框6.自...
    99+
    2024-04-02
  • Flutter Widgets 对话框-Dialog
    注意:无特殊说明,Flutter版本及Dart版本如下:Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0当应用程序进行重要操作时经常需要用户进行2次确认,以避免用户的误操作,比如删除文件时,一般会弹出提示“...
    99+
    2023-06-04
  • C#中的Dialog对话框
    一、MessageBox弹出框 MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,Messa...
    99+
    2024-04-02
  • Android自定义样式圆角dialog对话框
    本文实例为大家分享了Android创建自定义样式圆角dialog对话框的具体代码,供大家参考,具体内容如下 效果如上,圆角对话框,标题和正文都可以自己设定 做法: 1.在res文件...
    99+
    2024-04-02
  • Android AlertDialog(对话框)实例详解
    目录1.创建AlertDialog1.1 布局文件代码如下:1.2 MainActivity的主要代码如下所示:2.普通提示对话框3.普通列表对话框4.单选对话框4、复选对话框6、自...
    99+
    2024-04-02
  • Android自定义对话框Dialog的简单实现
    本文着重研究了自定义对话框,通过一下步骤即可清晰的理解原理,通过更改界面设置和style类型,可以应用在各种各样适合自己的App中。首先来看一下效果图:首先是activity的界面点击了上述图片的按钮后,弹出对话框:点击对话框的确定按钮:点...
    99+
    2023-05-30
    android 对话框 dialog
  • Android对话框使用方法详解
    对话框(Dialog)是Android系统在Activity或者其他组件运行过程中提供的一种提示机制。它可以帮助应用完成一些必要的提示功能,同时提供一些与用户交互的功能。 对话框分为...
    99+
    2024-04-02
  • Android怎么自定义样式圆角dialog对话框
    这篇文章主要介绍“Android怎么自定义样式圆角dialog对话框”,在日常操作中,相信很多人在Android怎么自定义样式圆角dialog对话框问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android...
    99+
    2023-06-25
  • 去除element-ui中Dialog对话框遮罩层方法详解
    目录前言modal 属性示例代码:前言 本文主要介绍了如何去除 element-ui 中 Dialog 对话框遮罩层的方法,并给出了示例代码以及页面效果作为参考。 modal 属性...
    99+
    2022-12-20
    element-ui Dialog遮罩层去除 element-ui Dialog
  • C#中的Dialog对话框怎么用
    这篇文章主要讲解了“C#中的Dialog对话框怎么用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#中的Dialog对话框怎么用”吧!一、MessageBox弹出框MessageBox.S...
    99+
    2023-06-30
  • C/C++Qt自定义Dialog对话框组件应用案例详解
    在上一篇文章 《C/C++ Qt 标准Dialog对话框组件应用》 中我给大家演示了如何使用Qt中内置的标准对话框组件实现基本的数据输入功能。 但有时候我们需要一次性修改多个数据,使...
    99+
    2024-04-02
  • WPF框架Prism中对话框Dialog用法介绍
    什么是Dialog 对话框实际上是我们应用程序经常用到的一个功能, 类如: Show、ShowDialog。 可以弹出一个我们指定的窗口, 仅此而已, 那么在Prism当中, Dia...
    99+
    2024-04-02
  • 如何在Android中利用Dialog实现一个对话框功能
    今天就跟大家聊聊有关如何在Android中利用Dialog实现一个对话框功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。一、普通对话框AlertDialog.Builder bui...
    99+
    2023-05-31
    android dialog roi
  • Android编程如何实现对话框Dialog背景透明功能
    这篇文章主要介绍Android编程如何实现对话框Dialog背景透明功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体如下:先看效果: 这是我做的一个拨号器强的面板,拨号的时候会查询手机中的联系人,显示...
    99+
    2023-05-30
    android dialog
  • Android对话框AlertDialog与DatePickerDialog及TimePickerDialog使用详解
    目录一、提醒对话框AlertDialog二、日期对话框DatePickerDialog第一种-点击选择日期出现日历第二种-滚动选择日期三、时间对话框TimePickerDialog方...
    99+
    2024-04-02
  • C/C++ Qt Dialog 对话框组件应用技巧
    在Qt中对话框分为两种形式,一种是标准对话框,另一种则是自定义对话框,在一般开发过程中标准对话框使用是最多的了,标准对话框一般包括 QMessageBox,QInputDialog,...
    99+
    2024-04-02
  • Android开发之缓冲dialog对话框创建、使用与封装操作
    本文实例讲述了Android开发之缓冲dialog对话框创建、使用与封装操作。分享给大家供大家参考,具体如下:package com.hstech.handysystem.prompt;import android.app.Dialog;i...
    99+
    2023-05-30
    android dialog 对话框
  • Android如何自定义升级对话框示例详解
    前言本文主要给大家介绍了关于Android自定义升级对话框的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。实现的效果如下所示其实这也只是一个DialogFragment 而已,重点只是在于界面的设计想要使用做出这...
    99+
    2023-05-30
    android 自定义 升级对话框
  • android对话框如何使用
    Android对话框可以通过AlertDialog类来使用,以下是一个简单的示例:1. 创建AlertDialog.Builder对...
    99+
    2023-08-23
    android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作