Android常用的AlertDialog对话框及自定义对话框
作者:甄情 发布时间:2021-05-31 05:09:51
标签:android,alertdialog
常用的Dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框
AlertDialog的常用方法
setTitle:为对话框设置标题
setMessage:为对话框设置内容
setIcon:为对话框设置图标
setItems设置对话框要显示的list
setMultiChoiceItems:一般用于复选框显示
setSingleChoiceItem:,设置单选按钮
setNeutralButton:普通按钮
setPositiveButton:添加确定按钮
setNegativeButton:添加取消按钮
setView:设置自定义样式
下面通过一个实例来了解这些方法
这是运行结果:
MainActivity.class
package com.example.alertdialog;
import android.R.bool;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.LabeledIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private int mark=0;
private String item[] = { "学生", "工人", "教师", "农民" };
private String multChoice[]={"旅游","电影","运动","读书"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btn_button1);
button2 = (Button) findViewById(R.id.btn_button2);
button3 = (Button) findViewById(R.id.btn_button3);
button4 = (Button) findViewById(R.id.btn_button4);
button5 = (Button) findViewById(R.id.btn_button5);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//确认对话框
case R.id.btn_button1: {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("确认对话框");
builder.setMessage("确认退出?");
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你单击了确定按钮",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你单击了取消按钮",
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
//列表对话框
case R.id.btn_button2: {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("职业");
builder.setItems(item, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你的职业是" + item[which],
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
//多选对话框
case R.id.btn_button3: {
final boolean choose[]=new boolean[multChoice.length];
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("爱好");
builder.setMultiChoiceItems(multChoice, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
choose[which]=isChecked;
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String result="";
for(int i=0;i<multChoice.length;i++){
if(choose[i]){
result+=multChoice[i]+" ";
}
}
Toast.makeText(MainActivity.this, "你的爱好["+result+"]", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
//单选对话框
case R.id.btn_button4: {
mark=0;
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("职业");
builder.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mark=which;
}
});
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你的职业:"+item[mark], Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
//自定义对话框
case R.id.btn_button5: {
LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.item, null);
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("自定义对话框");
builder.setView(view);
builder.setNeutralButton("普通按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"我是自定义的对话框哦",Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
}
}
}
布局文件
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"
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=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn_button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="确认对话框" />
<Button
android:id="@+id/btn_button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="列表对话框" />
<Button
android:id="@+id/btn_button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="多选对话框" />
<Button
android:id="@+id/btn_button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单选对话框" />
<Button
android:id="@+id/btn_button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定义对话框" />
</LinearLayout>
</RelativeLayout>
自定义的对话框布局文件
item.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" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/icon"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我是自定义的对话框"
/>
</LinearLayout>
来源:http://blog.csdn.net/su20145104009/article/details/50764612


猜你喜欢
- 1 Android SDK自带的org.json解析解析原理: 基于文档驱动,需要把全部文件读入到内存中,然后遍历所有数据,根据需要检索想要
- Android对sdcard扩展卡文件的操作其实就是普通的文件操作,但是仍然有些地方需要注意。比如:1.加入sdcard操作权限;2.确认s
- java中this与super关键字的使用方法这几天看到类在继承时会用到this和super,这里就做了一点总结,与各位共同交流,有错误请各
- 本文实例讲述了C#实现的Win32控制台线程计时器功能。分享给大家供大家参考,具体如下:在C#中提供了三种类型的计时器:1、基于 Windo
- 一、数据类型与变量的介绍在程序运行的过程中计算机需要记录大量的状态 数据(这里我们统称数据)。那这些数据都存放在哪呢?程序在运行过程中的数据
- SmartArt其实就是一个文字的可视化工具,用户可在PowerPoint,Word,Excel中使用该特性创建各种图形图表。SmartAr
- 一:需求当小数位很多的时候,小数位后面可能有一些多余的0并没有任何实际意义。所以在某些业务需求下可以去掉这些多余的0。例如:0.2000可以
- springboot http转https一、安全证书的生成可以使用jdk自带的证书生成工具,jdk自带一个叫keytool的证书管理工具,
- 一、DMI动态方法调用的其中一种改变form表单中action属性的方式已经讲过了。还有两种,一种是改变struts.xml配置文件中act
- 前言在日常开发中,圆形的图片效果还是很常见的。可以通过给Paint设置Xfermode来实现,这里简单记录如下。实现实现圆形效果的核心是Po
- 继承"基类"跟继承"接口"都能实现某些相同的功能,但有些接口能够完成的功能是只用基类无法实现的1.接
- Android设备的内存有限,对于大图片,必须进行压缩后再进行显示,否则会出现内存溢出:OOM;处理策略:1.使用缩略图(Thumbnail
- 本文实例讲述了基于WebClient实现Http协议的Post与Get对网站进行模拟登陆和浏览的方法。分享给大家供大家参考。具体分析如下:一
- 我们开发任何一个Spring Boot项目,都会用到如下的启动类@SpringBootApplication public class Ap
- 1、文件上传1.1 后端部分1.1.1 引入Apache Commons FIleUpload组件依赖<!--文件上传与下载相关的依赖
- 什么是序列化: 序列化就是把内存中的对象,转换成字节序列(或其他数据传输协议)以便于存储到磁盘(持久化)
- 问题描述涛涛最近要负责图书馆的管理工作,需要记录下每天读者的到访情况。每位读者有一个编号,每条记录用读者的编号来表示。给出读者的来访记录,请
- spring boot诞生的背景在spring boot出现以前,使用spring框架的程序员是这样配置web应用环境的,需要大量的xml配
- 本文实例讲述了java和javascript中过滤掉img形式的字符串不显示图片的方法。分享给大家供大家参考。具体实现方法如下:1. jav
- 介绍Java中介者模式(Mediator Pattern)是一种行为设计模式,它可以降低多个对象之间的耦合性,通过一个中介者对象来协调这些对