AlertDialog对话框的几种样式
2016-01-21 13:01
537 查看
使用手机的时候我们都注意到,经常会在界面上弹出一些对话框,比如简单的信息提示、询问用户是否退出或者让用户选择亦或者是输入一些简单的信息。这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,简单归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助。
通常在执行删除或者其他一些需要用户进行确认的时候出现。代码如下:
在特定情况下,除了需要有“确定”和“取消”两个按钮外还会有一个查看详情按钮。代码如下:
有时候我们在登录的时候会弹出一个对话框输入账号密码。此时就用到了自定义的对话框。
首先创建一个自定义的登录界面:
然后在弹出对话框的事件中对这个布局进行引用
4000
定义的一个布局文件,也可以自己传入一个文本编辑框,当然,你可以传入任何的视图对象,比如图片框,WebView等。。只有想不到~
例如:
传入文本框
传入图片
最后总结一下,android平台为我们开发提供了极大的便利,AlertDialog.Builder能做的不止这些,这里给大家展示的只是冰山一角,我们可以尽情的发挥想象,创造我们自己的对话框。
1.简单的信息提示框
此处setPositiveButton("确定", null)传入的null值是不实现“确认”的点击事件,具体怎么实现下面会有。AlertDialog.Builder dialog1=new AlertDialog.Builder(DialogActivity.this); dialog1.setTitle("标题").setMessage("这是一个简单的消息框!").setPositiveButton("确定", null).show();
2. 确认对话框
通常在执行删除或者其他一些需要用户进行确认的时候出现。代码如下:
AlertDialog.Builder dialog2=new AlertDialog.Builder(DialogActivity.this); dialog2.setTitle("提示").setMessage("确认删除?").setPositiveButton("确定", null).setNegativeButton("取消", null).show();
3.有多个按钮的对话框
在特定情况下,除了需要有“确定”和“取消”两个按钮外还会有一个查看详情按钮。代码如下:
AlertDialog.Builder dialog3=new AlertDialog.Builder(DialogActivity.this); dialog3.setTitle("警告").setMessage("确认删除?").setPositiveButton("确定", null).setNegativeButton("取消", null).setNeutralButton("查看详情", null).show();
4.多选项单选对话框
AlertDialog.Builder dialog5=new AlertDialog.Builder(DialogActivity.this); final String[] arrayString=new String[]{"张三","李四","王五","赵六","陈七","梁八"}; dialog5.setTitle("谁是程序员?"); dialog5.setIcon(R.drawable.ic_launcher).setItems(arrayString, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // which参数是当前点击的索引 Toast.makeText(DialogActivity.this, arrayString[which]+"是程序员", Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", null).show();
5.多选框对话框
AlertDialog.Builder dialog6=new AlertDialog.Builder(DialogActivity.this); final String[] arrayString1=new String[]{"张三","李四","王五","赵六","陈七","梁八"}; dialog6.setTitle("谁是程序员?"); dialog6.setIcon(R.drawable.ic_launcher).setMultiChoiceItems(arrayString1, null, null).setPositiveButton("确定", null).setNegativeButton("取消", null).show();
6.自定义对话框
有时候我们在登录的时候会弹出一个对话框输入账号密码。此时就用到了自定义的对话框。
首先创建一个自定义的登录界面:
<?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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" /> <EditText android:id="@+id/edt1" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1"> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" /> <EditText android:id="@+id/edt2" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1"> </EditText> </LinearLayout> </LinearLayout>
然后在弹出对话框的事件中对这个布局进行引用
LayoutInflater layoutInflater=LayoutInflater.from(DialogActivity.this); View loginView=layoutInflater.inflate(R.layout.login, null); AlertDialog.Builder dialog7=new AlertDialog.Builder(DialogActivity.this); dialog7.setTitle("登录").setView(loginView).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //在这里对“确定”事件进行处理 } }).setNegativeButton("取消", null).show();其中setView()是用来加载View的,可以是自己
4000
定义的一个布局文件,也可以自己传入一个文本编辑框,当然,你可以传入任何的视图对象,比如图片框,WebView等。。只有想不到~
例如:
传入文本框
传入图片
最后总结一下,android平台为我们开发提供了极大的便利,AlertDialog.Builder能做的不止这些,这里给大家展示的只是冰山一角,我们可以尽情的发挥想象,创造我们自己的对话框。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories