闲来无事,就练习了下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提示对话框练习的更多相关文章

  1. android中提示&对话框----AlertDialog

    AlertDialog(对话框) 一.对话框的基本使用流程 step1:创建AlertDialog.Buider; step2:调用setIcon()设置图标,setTitle()或者setCusto ...

  2. 自己主动更新--下载apk以及提示对话框的实现(3)

    下载apk以及提示对话框的实现 一.步骤: 1. 确定有能够更新的版本号,对话框提醒用户是否进行更新. 2. 选择更新的话,显示下载对话框而且进行下载.否则关闭提示更新对话框. 3. Apk下载完毕后 ...

  3. Android 自定义AlertDialog退出对话框

    Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...

  4. Ext信息提示对话框

    Ext.window.MessageBox是一个工具类,他继承自Ext.window.Windoe对象,用来生成各种风格的信息提示对话框,其实例对象可以通过Ext.MessageBox或Ext.Msg ...

  5. PermissionDialog【权限申请提示对话框】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 随着Android6.0的普及,权限申请也变成了我们开发中必写的一段代码.比如sd卡权限.定位权限.拍照权限,这些几乎都是每个app ...

  6. java基础--提示对话框的使用

    java基础--提示对话框的使用 2019-03-17-00:35:50-----云林原创 一.显示信息对话框:使用“JOptionPane.showMessageDialog”显示:   图标 对话 ...

  7. 44. Ext信息提示对话框

    转自:https://www.cnblogs.com/glsqh/p/5920500.html Ext.window.MessageBox是一个工具类,他继承自Ext.window.Windoe对象, ...

  8. ANDROID 系统提示对话框(ALERTDIALOG)的使用

    new AlertDialog.Builder(baseActivity).setTitle("删除确认")//设置对话框标题 .setMessage("您确定要删除选中 ...

  9. Android学习总结——系统提示对话框(AlertDialog)

    new AlertDialog.Builder(MainActivity.this).setTitle("退出")//设置对话框标题 .setMessage("官人可是要 ...

随机推荐

  1. __name__变量

    再写项目过程中,将功能函数写在 if  __name=="__main__":之外,执行代码写在之中,如下:如果在foo模块打印__name__为__main__,而如果在bin模 ...

  2. vue.js_11_路由的2中参数传递和路由的嵌套

    1.以?的形式传递参数   <router-link to="/login?id=10&name=zs">登录</router-link> 发送参数 ...

  3. leyou_06_图片上传至FastDFS

    1.推荐一个开源的FastDFS客户端,支持最新的SpringBoot2.0.配置使用极为简单,支持连接池,支持自动生成缩略图 1.1 在文件上传的微服务中 引入依赖 <dependency&g ...

  4. Joomla 随记

    使用 joomla 开发好几个网站了,记录一下基础的东西吧~ 一.下载: 进入 joomla官网 下载:   二.安装 Joomla: 把下载好的 joomla 放到网站文件夹(图为wamp阿帕奇服务 ...

  5. mysql高级教程(二)-----性能分析

    MySQL常见瓶颈 1.cpu SQL中对大量数据进行比较.关联.排序.分组 2.IO a.实例内存满足不了缓存数据或排序等需要,导致产生大量物理 IO. b.查询执行效率低,扫描过多数据行. 3.锁 ...

  6. 第三方博客同步xmlrpc、rest、API等相关的文章网址记录

    http://answers.microsoft.com/en-us/windowslive/forum/writer-wlsettings/i-am-encountering-a-problem-s ...

  7. scrapy中的Request和Response对象

    前言: 如果框架中的组件比做成是人的各个器官的话,那个Request和Response就是血液,Item就是代谢产物 Request对象: 是用来描述一个HTTP请求,其构造参数有 url 请求的UR ...

  8. xshell下载免费版

    正在使用的xshell 5不能使用,提示xshell 5评估期已过,重新安装还是不行.其实xshell 5有免费版的,即Home & school 版本.卸载原程序,下载安装免费版本的xshe ...

  9. input[type=file]上传图片及转为base64码以及预览

    <input type="file" id="imgurl" capture="camera" accept="image/ ...

  10. String--在内存中的表现

    创建字符串的方法有两种:   Stringstr1=”直接赋值法”          Stringstr2=new String(“通过new关键字的方法来创建”); 在执行String str1=” ...