原文:

http://blog.csdn.net/kkfdsa132/article/details/6322835

Android为我们提供几种对话框,主要有:AlertDialog、ProgressDialog、DataPickerDialog、TimePickerDialog。

AlertDialog,是具有0-3个按钮,还可以可以放置选项、复选框单选框或自定义布局等,以达到与用户交互的效果。

AlertDialog中 最常用的莫过于Builder对话框。下面,列举出它常用的几种方式:1.简单的Builder;2.带控件的Builder;3.自定义布局的Builder;4.嵌套的Builder

 1)简单的Builder

    

  1. new AlertDialog.Builder(AlertDialog_Builder.this)   //Content上下文
  2. .setIcon(android.R.drawable.ic_dialog_alert)        //图标,android.R开头表示Android提供的资源库
  3. .setTitle("简单的Builder:显示文本内容")              //标题
  4. .setMessage(et_show.getText())                      //要显示的内容
  5. .show();                                            //显示

  2)带控件的Builder

    

  1. final CharSequence[] items ={"Black", "Red", "Blue", "Yellow"};
  2. final int[] color = {Color.BLACK, Color.RED, Color.BLUE, Color.YELLOW};
  3. final StringBuffer selectedItem = new StringBuffer("0");
  4. new AlertDialog.Builder(AlertDialog_Builder.this)
  5. .setIcon(android.R.drawable.ic_dialog_info)
  6. .setTitle("带Button的Builder:显示文本内容")
  7. //.setMessage(et_show.getText())
  8. .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
  9. @Override
  10. public void onClick(DialogInterface dialog, int which) {
  11. // TODO Auto-generated method stub
  12. selectedItem.delete(0, selectedItem.length()-1);
  13. selectedItem.append(which);
  14. }
  15. })
  16. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {//添加一个Button并注册其响应事件
  17. @Override
  18. public void onClick(DialogInterface dialog, int which) {
  19. // TODO Auto-generated method stub
  20. int colorID = Integer.parseInt(selectedItem.toString());
  21. et_show.setBackgroundColor(color[colorID]);
  22. }
  23. })
  24. .setNegativeButton("清除", new DialogInterface.OnClickListener() {    //注意,这个是SetNegativeButton,上面那个是SetPositiveButton
  25. //不能重复名字,否则最后那个会覆盖前面那个,也就是说每个Buttion只显示一个。
  26. @Override
  27. public void onClick(DialogInterface dialog, int which) {
  28. // TODO Auto-generated method stub
  29. et_show.setText("");
  30. }
  31. })
  32. .setNeutralButton("取消", new DialogInterface.OnClickListener() {
  33. @Override
  34. public void onClick(DialogInterface dialog, int which) {
  35. // TODO Auto-generated method stub
  36. dialog.cancel();
  37. }
  38. })
  39. .show();

 3)自定义布局的Builder

    

a.代码

  1. final LayoutInflater layout = LayoutInflater.from(AlertDialog_Builder.this);    //用于获取,要使用final修饰
  2. final View customView = layout.inflate(R.layout.alter_text, null);          //创建自定义的View,要使用final修饰
  3. new AlertDialog.Builder(AlertDialog_Builder.this)
  4. .setIcon(android.R.drawable.ic_dialog_info)
  5. .setTitle("自定义的Builder:修改文本内容")
  6. .setView(customView)        //添加自定义视图
  7. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {//添加一个Button并注册其响应事件
  8. @Override
  9. public void onClick(DialogInterface dialog, int which) {
  10. // TODO Auto-generated method stub
  11. EditText et_alteredText = (EditText)customView.findViewById(R.id.et_alteredText); //获取自定义View里面的EditText
  12. et_show.setText(et_alteredText.getText().toString());
  13. }
  14. })
  15. .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { //注意,这个是SetNegativeButton,上面那个是SetPositiveButton
  16. //不能重复名字,否则最后那个会覆盖前面那个,也就是说每个Buttion只显示一个。
  17. @Override
  18. public void onClick(DialogInterface dialog, int which) {
  19. // TODO Auto-generated method stub
  20. dialog.cancel();
  21. }
  22. })
  23. .show();

b.布局文件alter_text.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout
  3. android:id="@+id/widget1"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7. >
  8. <EditText
  9. android:id="@+id/et_alteredText"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:textSize="18sp"
  13. android:layout_x="0px"
  14. android:layout_y="22px"
  15. >
  16. </EditText>
  17. </AbsoluteLayout>

 4)嵌套的Builder

   

  1. final LayoutInflater layout2 = LayoutInflater.from(AlertDialog_Builder.this);   //用于获取XML工厂,要使用final修饰
  2. final View customView2 = layout2.inflate(R.layout.alter_text, null);            //创建自定义的View,要使用final修饰
  3. new AlertDialog.Builder(AlertDialog_Builder.this)
  4. .setIcon(android.R.drawable.ic_dialog_info)
  5. .setTitle("嵌套的Builder:显示文本内容")
  6. .setMessage(et_show.getText())
  7. .setPositiveButton(R.string.rename, new DialogInterface.OnClickListener() {//添加一个Button并注册其响应事件
  8. @Override
  9. public void onClick(DialogInterface dialog, int which) {
  10. // TODO Auto-generated method stub
  11. new AlertDialog.Builder(AlertDialog_Builder.this)
  12. .setIcon(android.R.drawable.ic_dialog_info)
  13. .setTitle("嵌套的Builder:修改文本内容")
  14. .setView(customView2)       //添加自定义视图
  15. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {//添加一个Button并注册其响应事件
  16. @Override
  17. public void onClick(DialogInterface dialog, int which) {
  18. // TODO Auto-generated method stub
  19. EditText et_alteredText = (EditText)customView2.findViewById(R.id.et_alteredText); //获取自定义View里面的EditText
  20. et_show.setText(et_alteredText.getText().toString());
  21. }
  22. })
  23. .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { //注意,这个是SetNegativeButton,上面那个是SetPositiveButton
  24. //不能重复名字,否则最后那个会覆盖前面那个,也就是说每个Buttion只显示一个。
  25. @Override
  26. public void onClick(DialogInterface dialog, int which) {
  27. // TODO Auto-generated method stub
  28. dialog.cancel();
  29. }
  30. })
  31. .show();
  32. }
  33. })
  34. .setNegativeButton("取消", new DialogInterface.OnClickListener() {    //注意,这个是SetNegativeButton,上面那个是SetPositiveButton
  35. //不能重复名字,否则最后那个会覆盖前面那个,也就是说每个Buttion只显示一个。
  36. @Override
  37. public void onClick(DialogInterface dialog, int which) {
  38. // TODO Auto-generated method stub
  39. et_show.setText("");
  40. }
  41. })
  42. .show();

至于Builder的进一步使用,以后在补上。

 
0

Android之Builder对话框的一些常用方式的更多相关文章

  1. android 开发AlertDialog.builder对话框的实现

    AndroidAPI提供了Dialog对话框控件,但google明确指出不建议开发者只是使用Dialog来创建对话框,而应该自定义对话框或者使用API中提供的Dialog的子类,如AlertDialo ...

  2. Android 自学之对话框

    Android为我们提供了丰富的对话框支持,提供了四种常用的对话框: AlertDialog:功能丰富.实际应用最广泛的对话框. ProgressDialog:进度对话框,该对话框只用于简单的进度条封 ...

  3. Android中的几个基本常用控件

    Android 中常用的几大UI控件 1. TextView : 用于在界面中显示一段文本信息 <TextView android:id="@+id/text_view" / ...

  4. android中跨进程通讯的4种方式

    转自:http://blog.csdn.net/lyf_007217/article/details/8542359 帖子写的很好.看来一遍,试了一遍,感觉太有意义.必须转过来! android中跨进 ...

  5. Android UI系列--对话框(一)(AlertDialog,TimePickerDialog,DatePickerDialog,ProgressDialog)

    一.Dialog介绍 dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一个对话框并不会沾满我们整个的屏幕,并且通常用于模型事件当中需要用户做出一个决定后才会继续 ...

  6. 【转】Android播放音频MediaPlayer的几种方式介绍

    接下来笔者介绍一下Android中播放音频的几种方式,android.media包下面包含了Android开发中媒体类,当然笔者不会依次去介绍,下面介绍几个音频播放中常用的类: 1.使用MediaPl ...

  7. 【Android】Android 8种对话框(Dialog)

    1.写在前面 Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮).列表.单选.多选.等待.进度条.编辑.自定义等多种形式,将在第2部分介绍 ...

  8. 【Android】播放音频的几种方式介绍

    接下来笔者介绍一下Android中播放音频的几种方式,android.media包下面包含了Android开发中媒体类,当然笔者不会依次去介绍,下面介绍几个音频播放中常用的类: 1.使用MediaPl ...

  9. Java Android 注解(Annotation) 及几个常用开源项目注解原理简析

    不少开源库(ButterKnife.Retrofit.ActiveAndroid等等)都用到了注解的方式来简化代码提高开发效率. 本文简单介绍下 Annotation 示例.概念及作用.分类.自定义. ...

随机推荐

  1. 猴博士4小时讲完C语言视频教程

    猴博士4小时讲完C语言视频教程,一共有9节课. 目录结构如下: 目录:/2020030-猴博士4小时讲完C语言 [1G] ┣━━1.C语言基本语句(上)(更多资源访问:www.jimeng365.cn ...

  2. Springmvc-crud-01错误

    错误:无法显示图书列表内容 原因:获取存储域对象中的名字写错了 controller层: 前端页面: 解决方案:前后端的代码要保持一致(名字自己定义),写代码要细心 修改成功后的界面

  3. CentOS7.6配置ip

    查看CentOS版本信息 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Core) 配置ip [root@loca ...

  4. VM虚拟机黑屏 鼠标进不去

    #开始 可能是我脸黑吧 最近用虚拟机好几次都是黑屏 鼠标进不去 但是任务管理器显示确实有资源消耗 也就是说实际上应该是开机成功了(但是听不到声音 也许是没有开机吧) #解决方案 管理员权限打开 cmd ...

  5. BugReport-智慧农业APP

    1.展示的界面显示不全 bug Description: 测试环境:win10.工具eclipse: 测试步骤:打开运行程序后模拟器启动,第一个界面显示过几秒跳到了另一个界面,问题是第一个界面显示不全 ...

  6. windows centos php-beast 安装

    https://github.com/imaben/php-beast-binaries windows下 可以直接在这里下载dll 根据自己的php版本  还有是不是线程安全的 来选择下载对应的 放 ...

  7. 传奇身上装备升级系列脚本,以及UPGRADEITEMEX 脚本的详细参数解释

    UPGRADEITEMEX 脚本的详细参数解释如下: UPGRADEITEMEX 物品位置(0-12) 属性位置(0-14) 成功机率(0-100) 点数机率(0-255) 是否破碎(0,1) 物品位 ...

  8. 「CF1042F」Leaf Sets

    传送门 Luogu 解题思路 比较显然的一种做法: 我们把一个点的子树高度抠出来并排序记为 \(L_i\),找到最大的 \(i\) 使得 \(L_{i-1}+L_i\le K\). 于是我们把前 \( ...

  9. 基础_04_list and tuple

    一.list(列表) list是Python里的一种容器,里面可以存储多个任何类型的数据,长度也可以任意伸缩,可以像C语言中数组那样,按照索引下标获取对应的值.但数组是一个存储多个固定类型变量的连续内 ...

  10. java 协程

    协程是比线程更轻量级的程序处理单元,也可以说是运行在线程上的线程,由自己控制 1.适用于被阻塞的,且需要大量并发的场景. 2.不适用于,大量计算的多线程,遇到此种情况,更好实用线程去解决. 虽然Jav ...