Android中AlertDialog(对话框)的使用
来源:互联网 发布:贝莱隆人品 知乎 编辑:程序博客网 时间:2024/04/28 09:11
本人菜鸟一枚,初写博客,不喜勿喷。
一,普通的确定对话框
该效果是按下返回键,确保不误操作。
代码如下:
public void dialog(){//该方法是创建对话框的方法 new AlertDialog.Builder(this)//使用的建造者模式 .setTitle("提示") .setMessage("确认退出这个Activity吗?") .setIcon(R.mipmap.ic_launcher)//设置标题图标 .setPositiveButton("取消",null) .setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {//确定按钮的点击事件 finish();//finish当前这个Activity。 } }) .show(); }
二,单选的对话框
只能选择一项
代码如下
public void dialog(){ final String s[]=new String[]{"呵呵呵", "哈哈哈", "嘿嘿嘿"};//要显示的数组 new AlertDialog.Builder(this)//创建AlertDialog的对象 .setIcon(R.mipmap.ic_launcher)//设置标题图标 .setTitle("单选框") .setSingleChoiceItems(//设置选项为单选 s,//选项的内容(String数组中的) 1,//初始选中的项(初始选中为第一项“哈哈哈”,-1为都不选中) new DialogInterface.OnClickListener() {//选中监听 @Override public void onClick(DialogInterface dialog, int which) { num=which;//这里是选中的项,num是全局变量 } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定按钮的监听 @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, s[num]+"", Toast.LENGTH_SHORT).show();//Toast打印选中的String数组中的项 } }) .show(); }
三,多选对话框
可以多选
代码如下:
public void dialog(){ final boolean selected[]=new boolean[]{false,false,false}; new AlertDialog.Builder(this)//创建对话框 .setIcon(R.mipmap.ic_launcher)//标题图标 .setTitle("多选框") .setMultiChoiceItems(new String[]{"大老爷们", "抠脚女汉子", "洪兴十三妹"},//显示数据 selected,//初始选中对应的boolean类型数组。 new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) {//点击回调的监听 selected[which]=isChecked; } }) .setNegativeButton("确定", new DialogInterface.OnClickListener() {//确定键的监听 @Override public void onClick(DialogInterface dialog, int which) { Log.i("yyy",selected[0]+""+selected[1]+selected[2]+"hehehehe"); //选中的结果在boolean类型数组中(下标与数据源相对应)储存,true为选中,false为没选中。 } }) .setPositiveButton("取消",null) .show(); }
四,列表框
只能选择一项
代码如下:
public void dialog(){ final String s[]=new String[]{"小", "中", "大"};//数据源 new AlertDialog.Builder(this)//创建对象 .setIcon(R.mipmap.ic_launcher)//标题图标 .setTitle("列表框") .setItems(s,//设置数据源 new DialogInterface.OnClickListener() {//设置监听 @Override public void onClick(DialogInterface dialog, int which) {//which是所选的数据源的项 Toast.makeText(AlertDialogActivity.this,s[which], Toast.LENGTH_SHORT).show(); } }) .show(); }
五,三个按钮的对话框
这种三个按钮对于有些机型来说不是很美观(不对称)比如以下这样
(如果需要的话)个人觉得使用自定义对话框的比较好!
代码如下:
public void dialog(){ new AlertDialog.Builder(this)//标题栏对象 .setIcon(R.mipmap.ic_launcher) .setTitle("调查") .setMessage("对本开发者长相的评价") .setPositiveButton("可爱", new DialogInterface.OnClickListener() {//监听“可爱”按钮 @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "是个乖乖娃儿!", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("很帅", new DialogInterface.OnClickListener() {//监听“很帅”按钮 @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "帅呆了", Toast.LENGTH_SHORT).show(); } }) .setNeutralButton("超级帅", new DialogInterface.OnClickListener() {//监听“超级帅”按钮 @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "宇宙第一帅", Toast.LENGTH_SHORT).show(); } }) .show(); }
六,自定义对话框
界面比较丑,不要在意细节!
代码如下:
public void but7(){ View view = LayoutInflater.from(this).inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.asd));//把xml解析成View final EditText ed= (EditText) view.findViewById(R.id.edit); new AlertDialog.Builder(this) .setIcon(R.mipmap.ic_launcher) .setTitle("自定义") .setView(view)//设置自定义的View .setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定监听 @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this,ed.getText(), Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消",null) .show(); }
xml如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:id="@+id/asd" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_width="match_parent" android:layout_height="match_parent"><TextView android:layout_weight="0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入" /> <EditText android:id="@+id/edit" android:layout_weight="1" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入" /></LinearLayout>
哪里有错的地方也希望网友们多多指出 !!!
1 0
- Android中AlertDialog对话框的使用
- Android中AlertDialog(对话框)的使用
- 【Android】Android中AlertDialog对话框的使用实例
- Android中AlertDialog以及自定义对话框的使用
- 【Android】对话框AlertDialog的使用(一)
- Android之AlertDialog(弹出式对话框)的使用
- Android简单弹出对话框AlertDialog的使用
- android AlertDialog 对话框的基本使用
- AlertDialog对话框的使用
- AlertDialog对话框的使用
- 对话框AlertDialog的使用
- Android中AlertDialog的使用
- Android中AlertDialog的使用
- Android 中 AlertDialog的使用
- Android中AlertDialog的使用
- Android中显示AlertDialog对话框
- Android详细的对话框AlertDialog
- Android的对话框(AlertDialog)
- 本地网站域名配置
- php内核分析(三)-全局变量
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 服务端程序
- 从头到尾打印链表
- Android中AlertDialog(对话框)的使用
- phpstorm + xdebug 断点调试
- 基于用户兴趣挖掘的个性化搜索引擎的研究
- CountDownTimer用法
- PS破解
- Permission denied: user=anonymous, access=EXECUTE, inode=”/tmp”
- 框架图片和基本图片套路
- springMVC 访问静态资源 访问图片存放的路径:这个路径可以不是tomcat下的目录
- 常见正则表达式