原文:

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. SSHException: Error reading SSH protocol banner

    当我在使用ssh  远程connect 另一台机器的server 时出现了错误,错误如下,起初以为是自己代码写的有问题,后来本地了一下看了跑的没问题,我就开始根据报错去查寻原因, 起初在论坛博客看到这 ...

  2. 普及C组第二题(8.1)

    2000. [2015.8.6普及组模拟赛]Leo搭积木(brick) 题目: Leo是一个快乐的火星人,总是能和地球上的OIers玩得很high.         2012到了,Leo又被召回火星了 ...

  3. 喵星之旅-狂奔的兔子-myeclipse搭建ssm

    . 可以使用试用期限内的myeclipse,也可以找到有授权的机器进行操作.搭建好的项目框架可以直接移植到免费软件eclipse使用.或者直接购买myeclipse授权. 一.创建一个java web ...

  4. opencv:通道的分离与合并

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  5. Could not find result map com.youotech.tl_cons_credit_rating.entity.Result

    后端报错如下: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.yo ...

  6. oracle 密码过期问题

     密码过期问题: ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

  7. Typora自动生成标题编号

    1.要实现的效果 按照markdown语法输入  # 一级标题后,自动生成前面的编号 2.配置方法 2.1.进入目录 2.2.创建文件 2.3.编辑文件 base.user.css /** initi ...

  8. JS中的数组创建,初始化

    JS中没有专门的数组类型.但是可以在程序中利用预定义的Array对象及其方法来使用数组. 在JS中有三种创建数组的方法: var arr = new Array(1,2,3,4); var arr = ...

  9. ASP.NET Core搭建多层网站架构【10-使用JWT进行授权验证】

    2020/01/31, ASP.NET Core 3.1, VS2019, Microsoft.AspNetCore.Authentication.JwtBearer 3.1.1 摘要:基于ASP.N ...

  10. SpringBoot 配置 Redis 多缓存名(不同缓存名缓存失效时间不同)

    import com.google.common.collect.ImmutableMap; import org.springframework.cache.CacheManager; import ...