【Andriod-AlertDialog控件】 弹出对话框AlertDialog用法
Result:


Code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
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
{
Button buttonSure, buttonRadio, buttonCheck, buttonList, buttonCustom; private String[] sexList =
{ "男", "女" };// 单选列表
private String[] likeList =
{ "篮球", "足球", "打游戏", "听音乐", "看电影" };// 多选列表
private String[] itemList =
{ "项目经理", "策划", "测试", "美工", "程序员" };// 列表 @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); buttonSure = (Button) findViewById(R.id.buttonSure);
buttonRadio = (Button) findViewById(R.id.buttonRadio);
buttonCheck = (Button) findViewById(R.id.buttonCheck);
buttonList = (Button) findViewById(R.id.buttonList);
buttonCustom = (Button) findViewById(R.id.buttonCustom); buttonSure.setOnClickListener(this);
buttonRadio.setOnClickListener(this);
buttonCheck.setOnClickListener(this);
buttonList.setOnClickListener(this);
buttonCustom.setOnClickListener(this);
} @Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button: break;
case R.id.buttonSure:
sureDialog();
break;
case R.id.buttonRadio:
radioDialog();
break;
case R.id.buttonCheck:
checkDailog();
break;
case R.id.buttonList:
listDialog();
break;
case R.id.buttonCustom:
customDialog();
break;
default:
break;
}
} /**
* 确认对话框
*/
private void sureDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("确认对话框");// 设置标题
builder.setIcon(R.drawable.ic_launcher);// 设置图标
builder.setMessage("确认对话框内容");// 设置内容
/* 添加对话框中确定按钮和点击事件 */
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
}
});
/* 添加对话框中取消按钮和点击事件 */
builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();// 获取dialog
dialog.show();// 显示对话框
} /**
* 单选确认对话框
*/
private void radioDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("性别");// 设置标题
builder.setIcon(R.drawable.ic_launcher);// 设置图标
/* 参数一位单选列表文字,参数二为默认第几个选中(-1默认不选中),参数三是创建监听器 */
builder.setSingleChoiceItems(sexList, -1, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
String sex = sexList[which];
Toast.makeText(MainActivity.this, "这个人性别为" + sex, Toast.LENGTH_SHORT).show();
}
}); /* 添加对话框中取消按钮和点击事件 */
builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();// 关闭对话框
}
});
AlertDialog dialog = builder.create();// 获取dialog
dialog.show();// 显示对话框
} /**
* 多选对话框
*/
private void checkDailog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("爱好");// 设置标题
builder.setIcon(R.drawable.ic_launcher);// 设置图标
/* 参数同单选对话框一样,另外第二个参数默认不选中为null,而不是-1 */
builder.setMultiChoiceItems(likeList, null, new DialogInterface.OnMultiChoiceClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked)
{
if (isChecked)
{
Toast.makeText(MainActivity.this, "我喜欢" + likeList[which], Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(MainActivity.this, "我不喜欢" + likeList[which], Toast.LENGTH_SHORT).show();
}
}
});
/* 添加对话框中取消按钮点击事件 */
builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();// 关闭对话框
}
});
AlertDialog dialog = builder.create();// 获取dialog
dialog.show();// 显示对话框
} /**
* 列表对话框
*/
private void listDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("部门列表");// 设置标题
builder.setIcon(R.drawable.ic_launcher);// 设置图标
builder.setItems(itemList, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "我点击了" + itemList[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();// 获取dialog
dialog.show();// 显示对话框
} /**
* 自定义对话框
*/
private void customDialog()
{
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog, null);// 获取自定义布局
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("自定义对话框");// 设置标题
builder.setIcon(R.drawable.ic_launcher);// 设置图标
builder.setView(view);// 设置自定义样式布局到对话框
AlertDialog dialog = builder.create();// 获取dialog
dialog.show();// 显示对话框
} }
【Andriod-AlertDialog控件】 弹出对话框AlertDialog用法的更多相关文章
- 根据条件决定My97DatePicker日期控件弹出的日期格式
代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- 【转】javascript入门系列演示·三种弹出对话框的用法实例
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- 【JSP】三种弹出对话框的用法实例
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- javascript入门系列演示·三种弹出对话框的用法实例
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- 安卓弹出对话框——Alertdialog
在Android开发当中,在界面上弹出一个Dialog对话框使我们经常需要做的,本篇随笔将详细的讲解Dialog对话框这个概念,包括定义不同样式的对话框. 一.Dialog 我们首先来看看androi ...
- 安卓弹出对话框——Alertdialog(一)
首先看各种样式的对话框: 我们看到,Dialog有很多的子类实现,所以我们要定义一个对话框,使用其子类来实例化一个即可,而不要直接使用Dialog这个父类来构造. 二.AlertDialog 今天我们 ...
- my97日期控件弹出位置显示异常
使用my97日期选择控件的时候,如果整个页面是有滚动条的,根据触发显示日期的控件的父控件的position不同会显示不同的情况 1.position不为fixed则滑动滚动条,显示的日期层不会出现异常 ...
- JSP中三种弹出对话框的用法《转》
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- 安卓弹出对话框——AlertDialog(二)
在Android中,启动一个对话框有三种方式: 1.定义一个新的activity,并将其主题设置为对话框风格 2.使用AlertDialog类,并且显示它 3.使用 Android的Dialog类的子 ...
随机推荐
- django F与Q查询 事务 only与defer
F与Q 查询 class Product(models.Model): name = models.CharField(max_length=32) #都是类实例化出来的对象 price = mode ...
- 树莓派驱动开发 helloworld
编写Makefile ifneq ($(KERNELRELEASE),) obj-m := MiniX.o else KDIR := /home/hi/pi/kernel/linux/ all: ma ...
- Java面试知识点汇总
Java面试知识点汇总 置顶 2019年05月07日 15:36:18 温柔的谢世杰 阅读数 21623 文章标签: 面经java 更多 分类专栏: java 面试 Java面试知识汇总 版权声明 ...
- 树莓派和STM32通过USB和串口通信记录
不管怎样,为了简便开发,通信选择串口通信. 推荐文章:https://blog.csdn.net/magnetoooo/article/details/53564797 推荐测试工具:https:// ...
- PyQt5创建多线程
参阅: https://blog.csdn.net/chengmo123/article/details/96477103 https://www.cnblogs.com/zhuminghui/p/9 ...
- Fabric的简介
1,初识fabric 1,什么是fabric fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理的效率. 简单来说: (1)一个让你通过命令行执行无参数python ...
- CSS中为什么有的元素能够设置高度,而有的元素却不能设置高度与宽度?
可以使用{display:block}将内联元素变为块级元素,同时使用{display:inline}将块级元素变为内联元素. {display:inline-block}又是怎么回事,根据张鑫旭老师 ...
- vue2中的keep-alive使用总结及注意事项
问题总结;最近在写vue移动端的项目的时候,当我切换菜单,再切换换回去的时候,发现页面出现闪动的效果,其原因是因为切换回去之后,页面重新渲染了;为了解决这一问题:查阅资料,只需要在 入口文件 App. ...
- 深入分析 Docker 镜像原理
摘要:近日, DaoCloud 软件工程师孙宏亮在 CSDN Container 微信群为大家带来了 Docker 镜像原理的深度分享,本次分享的重点是 Docker 镜像,分享的内容主要包含两个部分 ...
- docker容器里面安装php的redis扩展
docker exec -i -t php /bin/bash 进入php容器内执行:pecl install -o -f redis 修改php.ini,添加:extension=redis. ...