AlertDialog提示对话框练习
闲来无事,就练习了下AlertDialog对话框。
首先写了三个button,分别打开一般对话框,单选列表对话框和复选对话框。
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"
tools:context=".MainActivity"
android:orientation="vertical"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示对话框"
android:onClick="listener1"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选对话框"
android:onClick="listener2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复选对话框"
android:onClick="listener3"/>
</LinearLayout>
java代码:
package com.dj.alertdialogtest; 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.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity {
AlertDialog.Builder dialog=null;
private int num; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} /**显示对话框*/
public void listener1(View v){
dialog=new AlertDialog.Builder(MainActivity.this);//初始化显示对话框
dialog.setIcon(R.drawable.ic_launcher);//设置对话框的图标
dialog.setTitle("调查");//设置对话框的标题
dialog.setMessage("假如给你一百万你想做什么?");//设置对话框的内容信息 /**设置对话的三个button按钮*/
dialog.setPositiveButton("捐款",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "我要做有意思的事!", Toast.LENGTH_LONG).show();
}
});
dialog.setNegativeButton("旅游", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "我要好好活!", Toast.LENGTH_LONG).show();
}
}); dialog.setNeutralButton("坐吃等死", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "我要坐吃等死!", Toast.LENGTH_LONG).show();
}
});
dialog.create();//创建
dialog.show();//将设置完的对话框显示出来
} /**以下是单选列表对话框*/
public void listener2(View v){
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
final String[] str={"鱼香肉丝","宫保鸡丁","回锅肉","东坡肉"};//设置列表的元素
dialog.setIcon(R.drawable.ic_launcher);//设置图标
dialog.setTitle("选择你喜欢的食物");//设置标题
/**设置单选列表对话框。第一个参数为列表元素,以字符串数组承装;
* 第二个参数是默认选中的元素,0表示第一个*/
dialog.setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
num=which;//which表示哪个下标被选中
}
});
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String temp="你喜欢的食物是:";
//str[num]表示选中的是哪个食物
Toast.makeText(getApplicationContext(), temp+str[num], Toast.LENGTH_LONG).show();
}
});
dialog.create();
dialog.show();
} /**复选列表对话框*/
public void listener3(View v){
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
final String[] str={"王力宏","周杰伦","王菲","那英"};
final boolean[] flags={true,false,true,false};
dialog.setIcon(R.drawable.ic_launcher);//设置图标
dialog.setTitle("你喜欢的歌手是");
dialog.setMultiChoiceItems(str, flags, new DialogInterface.OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface arg0, int which, boolean isChecked) {
flags[which]=isChecked;//哪一项被选中
}
});
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
String temp="你喜欢的歌手是:";
for (int i = 0; i < flags.length; i++) {
if(flags[i]){
temp+=str[i];
}
}
Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_LONG).show();
}
});
dialog.create();
dialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
演示效果:
AlertDialog提示对话框练习的更多相关文章
- android中提示&对话框----AlertDialog
AlertDialog(对话框) 一.对话框的基本使用流程 step1:创建AlertDialog.Buider; step2:调用setIcon()设置图标,setTitle()或者setCusto ...
- 自己主动更新--下载apk以及提示对话框的实现(3)
下载apk以及提示对话框的实现 一.步骤: 1. 确定有能够更新的版本号,对话框提醒用户是否进行更新. 2. 选择更新的话,显示下载对话框而且进行下载.否则关闭提示更新对话框. 3. Apk下载完毕后 ...
- Android 自定义AlertDialog退出对话框
Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...
- Ext信息提示对话框
Ext.window.MessageBox是一个工具类,他继承自Ext.window.Windoe对象,用来生成各种风格的信息提示对话框,其实例对象可以通过Ext.MessageBox或Ext.Msg ...
- PermissionDialog【权限申请提示对话框】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 随着Android6.0的普及,权限申请也变成了我们开发中必写的一段代码.比如sd卡权限.定位权限.拍照权限,这些几乎都是每个app ...
- java基础--提示对话框的使用
java基础--提示对话框的使用 2019-03-17-00:35:50-----云林原创 一.显示信息对话框:使用“JOptionPane.showMessageDialog”显示: 图标 对话 ...
- 44. Ext信息提示对话框
转自:https://www.cnblogs.com/glsqh/p/5920500.html Ext.window.MessageBox是一个工具类,他继承自Ext.window.Windoe对象, ...
- ANDROID 系统提示对话框(ALERTDIALOG)的使用
new AlertDialog.Builder(baseActivity).setTitle("删除确认")//设置对话框标题 .setMessage("您确定要删除选中 ...
- Android学习总结——系统提示对话框(AlertDialog)
new AlertDialog.Builder(MainActivity.this).setTitle("退出")//设置对话框标题 .setMessage("官人可是要 ...
随机推荐
- 廖雪峰Java11多线程编程-2线程同步-1同步代码块
1.线程安全问题 多个线程同时运行,线程调度由操作系统决定,程序本身无法决定 如果多个线程同时读写共享变量,就可能出现问题 class AddThread extends Thread{ public ...
- Redis高可用及集群
目录 Redis主从复制 环境准备 主从复制命令 Redis Sentinel 功能 Redis Sentinel配置 Redis集群 Redis主从复制 使用异步复制 一个服务器可以有多个从服务器 ...
- Web前后端缓存技术(缓存的主要作用是什么)
Web前后端缓存技术Web前后端缓存技术(缓存的主要作用是什么) 一.总结 一句话总结: 加快页面打开速度 减少网络带宽消耗 降低服务器压力 1.在Web应用中,应用缓存的地方有哪些? 主要有浏览器缓 ...
- 微信小程序上传图片(附后端代码)
几乎每个程序都需要用到图片. 在小程序中我们可以通过image组件显示图片. 当然小程序也是可以上传图片的,微信小程序文档也写的很清楚. 上传图片 首先选择图片 通过wx.chooseImage(OB ...
- python 可迭代对象,迭代器,生成器的区别及使用
可迭代对象 可迭代对象类型:list,dict,tuple,str,set,deque等 如何判断一个对象是否是可迭代对象,可以通过dir()方法看它里面有没有__iter__方法,如果有这个方法就是 ...
- 搭建maven聚合工程包含springboot模块
一.新建一个maven项目 二.删除src 打开pom.xml 补充标签 <packaging>pom</packaging> 新建 <module>brr- ...
- 洛谷P3306 随机数生成器
题意:给你一个数列,a1 = x,ai = (A * ai-1 + B) % P,求第一个是t的是哪一项,或者永远不会有t. 解:循环节不会超过P.我们使用BSGS的思想,预处理从t开始跳√P步的,插 ...
- light oj 1149 Factors and Multiples(二分匹配)
LightOJ1149 :Factors and Multiples 时间限制:2000MS 内存限制:32768KByte 64位IO格式:%lld & %llu 描述 You w ...
- message d:\WEB_APP_QuChongFu\file\五月.xlsx (文件名、目录名或卷标语法不正确。)
原因是 文件名或文件夹名中不能出现以下字符:\ / : * ? " < > | 但是后台读取到的附件的文件路径就是这样的 网上大佬说了,这样处理 rep ...
- Django ORM中的查询,删除,更新操作
ORM查询操作 修改views.py文件 from django.shortcuts import render, HttpResponse from app01 import models from ...