在android开发中,经常会用到对话框跟用户进行交互,方便用户可操作性;接下来就对常用对话框进行简单封装,避免在项目中出现冗余代码,加重后期项目的维护量;代码如有问题欢迎大家拍砖指正一起进步。

先贴出演示结果,在晒出演示代码。
1、运行成功后,原始界面如下:

2、点击“显示普通对话框”,效果界面如下:

3、点击“显示列表对话框”,效果界面如下:

4、点击“显示单选按钮对话框”,效果界面如下:

5、点击“显示复选对话框”,效果界面如下:

代码:
1、项目目录结构如下

2、对话框封装类DialogTool

  1. package com.hrtx.util;
  2.  
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface.OnClickListener;
  6.  
  7. /**
  8.  * 对话框封装类
  9.  *
  10.  * @author jiqinlin
  11.  *
  12.  */
  13. public class DialogTool {
  14.   
  15.     /**
  16.      * 创建普通对话框
  17.      *
  18.      * @param ctx 上下文 必填
  19.      * @param iconId 图标,如:R.drawable.icon 必填
  20.      * @param title 标题 必填
  21.      * @param message 显示内容 必填
  22.      * @param btnName 按钮名称 必填
  23.      * @param listener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
  24.      * @return
  25.      */
  26.  public static Dialog createNormalDialog(Context ctx,
  27.    int iconId,
  28.    String title,
  29.    String message,
  30.    String btnName,
  31.    OnClickListener listener) {
  32.   Dialog dialog=null;
  33.   android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
  34.   // 设置对话框的图标
  35.   builder.setIcon(iconId);
  36.   // 设置对话框的标题
  37.   builder.setTitle(title);
  38.   // 设置对话框的显示内容
  39.   builder.setMessage(message);
  40.   // 添加按钮,android.content.DialogInterface.OnClickListener.OnClickListener
  41.   builder.setPositiveButton(btnName, listener);
  42.   // 创建一个普通对话框
  43.   dialog = builder.create();
  44.   return dialog;
  45.  }
  46.   
  47.   
  48.     /**
  49.      * 创建列表对话框
  50.      *
  51.      * @param ctx 上下文 必填
  52.      * @param iconId 图标,如:R.drawable.icon 必填
  53.      * @param title 标题 必填
  54.      * @param itemsId 字符串数组资源id 必填
  55.      * @param listener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
  56.      * @return
  57.      */
  58.  public static Dialog createListDialog(Context ctx,
  59.    int iconId,
  60.    String title,
  61.    int itemsId,
  62.    OnClickListener listener) {
  63.   Dialog dialog=null;
  64.   android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
  65.   // 设置对话框的图标
  66.   builder.setIcon(iconId);
  67.   // 设置对话框的标题
  68.   builder.setTitle(title);
  69.   // 添加按钮,android.content.DialogInterface.OnClickListener.OnClickListener
  70.   builder.setItems(itemsId, listener);
  71.   // 创建一个列表对话框
  72.   dialog = builder.create();
  73.   return dialog;
  74.  }
  75.   
  76.     /**
  77.      * 创建单选按钮对话框
  78.      *
  79.      * @param ctx 上下文 必填
  80.      * @param iconId 图标,如:R.drawable.icon 必填
  81.      * @param title 标题 必填
  82.      * @param itemsId 字符串数组资源id 必填
  83.      * @param listener 单选按钮项监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
  84.      * @param btnName 按钮名称 必填
  85.      * @param listener2 按钮监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
  86.      * @return
  87.      */
  88.  public static Dialog createRadioDialog(Context ctx,
  89.    int iconId,
  90.    String title,
  91.    int itemsId,
  92.    OnClickListener listener,
  93.    String btnName,
  94.    OnClickListener listener2) {
  95.   Dialog dialog=null;
  96.   android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
  97.   // 设置对话框的图标
  98.   builder.setIcon(iconId);
  99.   // 设置对话框的标题
  100.   builder.setTitle(title);
  101.   // 0: 默认第一个单选按钮被选中
  102.   builder.setSingleChoiceItems(itemsId, 0, listener);
  103.   // 添加一个按钮
  104.   builder.setPositiveButton(btnName, listener2) ;
  105.   // 创建一个单选按钮对话框
  106.   dialog = builder.create();
  107.   return dialog;
  108.  }
  109.   
  110.   
  111.     /**
  112.      * 创建复选对话框
  113.      *
  114.      * @param ctx 上下文 必填
  115.      * @param iconId 图标,如:R.drawable.icon 必填
  116.      * @param title 标题 必填
  117.      * @param itemsId 字符串数组资源id 必填
  118.      * @param flags 初始复选情况 必填
  119.      * @param listener 单选按钮项监听器,需实现android.content.DialogInterface.OnMultiChoiceClickListener接口 必填
  120.      * @param btnName 按钮名称 必填
  121.      * @param listener2 按钮监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
  122.      * @return
  123.      */
  124.  public static Dialog createCheckBoxDialog(Context ctx,
  125.    int iconId,
  126.    String title,
  127.    int itemsId,
  128.    boolean[] flags,
  129.    android.content.DialogInterface.OnMultiChoiceClickListener listener,
  130.    String btnName,
  131.    OnClickListener listener2) {
  132.   Dialog dialog=null;
  133.   android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
  134.   // 设置对话框的图标
  135.   builder.setIcon(iconId);
  136.   // 设置对话框的标题
  137.   builder.setTitle(title);
  138.   builder.setMultiChoiceItems(itemsId, flags, listener);
  139.   // 添加一个按钮
  140.   builder.setPositiveButton(btnName, listener2) ;
  141.   // 创建一个复选对话框
  142.   dialog = builder.create();
  143.   return dialog;
  144.  }
  145. }

3、对话框Activity类DialogActivity

  1. package com.ljq.activity;
  2.  
  3. import android.app.Activity;
  4. import android.app.Dialog;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. /**
  13.  * 对话框Activity类
  14.  *
  15.  * @author jiqinlin
  16.  *
  17.  */
  18. public class DialogActivity extends Activity {
  19.  private boolean[] flags=new boolean[]{false,true,false}; //初始复选情况
  20.  private String[] items=null;
  21.   
  22.     private EditText etText=null;
  23.     private Button btnNormal=null;
  24.     private Button btnList=null;
  25.     private Button btnRadio=null;
  26.     private Button btnCheckBox=null;
  27.      
  28.     private static final int DIALOG_NORMAL=0; //普通对话框常量
  29.     private static final int DIALOG_LIST=1; //列表对话框常量
  30.     private static final int DIALOG_RADIO=2; //单选按钮对话框常量
  31.     private static final int DIALOG_CHECKBOX=3; //复选对话框常量
  32.      
  33.     @Override
  34.     public void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.main);
  37.          
  38.         items=getResources().getStringArray(R.array.hobby);
  39.          
  40.         etText=(EditText)findViewById(R.id.etText);
  41.         btnNormal=(Button)findViewById(R.id.btnNormal);
  42.         btnList=(Button)findViewById(R.id.btnList);
  43.         btnRadio=(Button)findViewById(R.id.btnRadio);
  44.         btnCheckBox=(Button)findViewById(R.id.btnCheckBox);
  45.         btnNormal.setOnClickListener(l);
  46.         btnList.setOnClickListener(l);
  47.         btnRadio.setOnClickListener(l);
  48.         btnCheckBox.setOnClickListener(l);
  49.     }
  50.      
  51.     @Override
  52.     protected Dialog onCreateDialog(int id) {
  53.      Dialog dialog=null;
  54.      switch (id) {
  55.   case DIALOG_NORMAL: //创建普通对话框
  56.    dialog = DialogTool.createNormalDialog(this,
  57.      R.drawable.icon,
  58.      "普通对话框",
  59.      "这是普通对话框中的内容!",
  60.      " 确 定 ",
  61.         new android.content.DialogInterface.OnClickListener(){
  62.       @Override
  63.       public void onClick(DialogInterface dialog, int which) {
  64.        etText.setText("这是普通对话框中的内容!");
  65.        return;
  66.       }
  67.               }
  68.             );
  69.    break;
  70.   case DIALOG_LIST: // 创建列表对话框
  71.    dialog = DialogTool.createListDialog(this,
  72.      R.drawable.icon,
  73.      "列表对话框",
  74.      R.array.hobby,
  75.         new android.content.DialogInterface.OnClickListener(){
  76.       @Override
  77.       public void onClick(DialogInterface dialog, int which) {
  78.        String hoddy=getResources().getStringArray(R.array.hobby)[which];    
  79.        etText.setText("您选择了: "+hoddy);
  80.        return;
  81.       }
  82.               }
  83.             );
  84.    break;
  85.   case DIALOG_RADIO: // 创建单选按钮对话框
  86.    dialog=DialogTool.createRadioDialog(this,
  87.      R.drawable.icon,
  88.      "单选按钮对话框",
  89.      R.array.hobby,
  90.         new android.content.DialogInterface.OnClickListener() {
  91.       @Override
  92.       public void onClick(DialogInterface dialog, int which) {
  93.        String hoddy = getResources().getStringArray(
  94.          R.array.hobby)[which];
  95.        etText.setText("您选择了: " + hoddy);
  96.        return;
  97.       }
  98.      },
  99.         " 确 定 ",
  100.            new android.content.DialogInterface.OnClickListener() {
  101.       @Override
  102.       public void onClick(DialogInterface dialog, int which) {
  103.        Toast.makeText(DialogActivity.this,
  104.          "您按了确定按钮!", Toast.LENGTH_LONG).show();
  105.        return;
  106.       }
  107.      }
  108.    );
  109.    break;
  110.   case DIALOG_CHECKBOX: // 创建复选框对话框
  111.    dialog=DialogTool.createCheckBoxDialog(this,
  112.      R.drawable.icon,
  113.      "复选对话框",
  114.      R.array.hobby,
  115.      flags,
  116.      new DialogInterface.OnMultiChoiceClickListener() {
  117.       public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  118.        flags[which] = isChecked;
  119.        String result = "您选择了:";
  120.        for (int i = 0; i < flags.length; i++) {
  121.         if (flags[i]) {
  122.          result = result + items[i] + "、";
  123.         }
  124.        }
  125.        etText.setText(result.substring(0, result.length() - 1));
  126.       }
  127.      },
  128.      " 确 认 ",
  129.            new android.content.DialogInterface.OnClickListener() {
  130.       @Override
  131.       public void onClick(DialogInterface dialog, int which) {
  132.        Toast.makeText(DialogActivity.this, "您按了确定按钮!", Toast.LENGTH_LONG).show();
  133.        return;
  134.       }
  135.      }
  136.        
  137.    );
  138.    break;
  139.   }
  140.      return dialog;
  141.     }
  142.      
  143.     //按钮监听
  144.     View.OnClickListener l = new View.OnClickListener() {
  145.   @Override
  146.   public void onClick(View v) {
  147.    Button btn = (Button) v;
  148.    switch (btn.getId()) {
  149.    case R.id.btnNormal: //普通对话框
  150.                 //显示对话框
  151.     showDialog(DIALOG_NORMAL);
  152.     break;
  153.    case R.id.btnList: //列表对话框
  154.                 //显示对话框
  155.     showDialog(DIALOG_LIST);
  156.     break;
  157.    case R.id.btnRadio: //单选按钮对话框
  158.                 //显示对话框
  159.     showDialog(DIALOG_RADIO);
  160.     break;
  161.    case R.id.btnCheckBox: //复选对话框
  162.                 //显示对话框
  163.     showDialog(DIALOG_CHECKBOX);
  164.     break;
  165.    }
  166.   }
  167.  };
  168. }

4、布局文件main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">
  5.     <EditText android:text=""
  6.         android:id="@+id/etText"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:editable="false"
  10.         android:cursorVisible="false" />
  11.     <Button android:text="显示普通对话框"
  12.         android:id="@+id/btnNormal"
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content" />
  15.     <Button android:text="显示列表话框"
  16.         android:id="@+id/btnList"
  17.         android:layout_width="fill_parent"
  18.         android:layout_height="wrap_content" />
  19.     <Button android:text="显示单选按钮对话框"
  20.         android:id="@+id/btnRadio"
  21.         android:layout_width="fill_parent"
  22.         android:layout_height="wrap_content" />
  23.     <Button android:text="显示复选对话框"
  24.         android:id="@+id/btnCheckBox"
  25.         android:layout_width="fill_parent"
  26.         android:layout_height="wrap_content" />
  27. </LinearLayout>

5、数组变量array.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. resources的使用
  4.  
  5. resoureces就是res目录下的那些目录和文件,常用的有:
  6. res/drawable/ 用来存放图片文件
  7. res/layout/ 用来存放布局定义文件
  8. res/values/ 用来存放一些变量、参数等文件
  9. -->
  10. <resources>
  11.  <string-array name="hobby">
  12.   <item>游泳</item>
  13.   <item>打篮球</item>
  14.   <item>登山</item>
  15.  </string-array>
  16. </resources>

6、清单文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.ljq.activity"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".DialogActivity"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.  
  15.     </application>
  16.     <uses-sdk android:minSdkVersion="4" />
  17.  
  18. </manifest>

完毕!!!!!

android常用对话框封装的更多相关文章

  1. Android 常用对话框Dialog封装

    Android 6种 常用对话框Dialog封装 包括: 消息对话框.警示(含确认.取消)对话框.单选对话框. 复选对话框.列表对话框.自定义视图(含确认.取消)对话框 分别如下图所示:       ...

  2. (转载)Android常用的Dialog对话框用法

    Android常用的Dialog对话框用法 Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的 ...

  3. Android常用酷炫控件(开源项目)github地址汇总

    转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...

  4. Android 常用炫酷控件(开源项目)git地址汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

  5. Android常用组件

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  6. Android常用组件【转】

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  7. android sqlite数据库封装 实现crud

    android常用的数据保存方式有文件.sharepreferences.数据库.网络.contentprovider集中方式. 文件存储方式,经常使用在缓存整个页面数据,比如电子书内容.html数据 ...

  8. Android 常用 adb 命令总结

    Android 常用 adb 命令总结 针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb 命令熟记于心, 将会为 Android 测试带来很大的方便,其中很多命 ...

  9. Android 常用代码大集合 [转]

    [Android]调用字符串资源的几种方法   字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...

随机推荐

  1. Js注释

    注释 介绍 作用 合作分享:方便他人阅读,便于分享 沉淀总结:容易忘记代码,自己总结沉淀 形式 1.// 双斜杠 2./**/斜杠星号 常用标签 标签 描述 @module 标明当前文件模块,在这个文 ...

  2. Ubuntu ( Linux) Eclipse 乱码问题

    刚装完Ubuntu,导入Java和Android项目时,发现字符乱码,究其原因,是由于Windows下使用的是GBK编码,而Ubuntu使用的是UTF-8编码.网上查找了相关资料,主要解决方案有两种. ...

  3. Linux内存管理原理

    本文以32位机器为准,串讲一些内存管理的知识点. 1. 虚拟地址.物理地址.逻辑地址.线性地址 虚拟地址又叫线性地址.linux没有采用分段机制,所以逻辑地址和虚拟地址(线性地址)(在用户态,内核态逻 ...

  4. js的json转换

    静态页面是: data:[{ value:2.5, itemStyle:{ normal:{color:'#4a90e2'} } },{ value:2.5, itemStyle:{ normal:{ ...

  5. JS不用通过其他转换两个小数加减得到正确答案

    之前写过一篇文章js比较两个属于float类型的小数,都需要通过某种函数转换下,太麻烦了,比如: 减法:10.2345-0.01=10.2245,这是正确的答案,但是当你做加法的时候就变了 加法:10 ...

  6. jquery this 与javascript的this

    <div class="list"> <table> <thead> <tr> <th width="110&quo ...

  7. CentOS 7.0 部署 Django 到运行起来第一个web service

    最近在学习Python,今天发现Django如此强大的web框架,不得不来试一试. 1. 安装Python,官网建议用Python3:

  8. xmpp的bug

    [微分享]:事前必三思,事中要坚韧,事后莫悔恨,只有眼光看远些,脚步坚实些,人生方多些圆满,少些遗憾. xmpp的bug

  9. Sightseeing(poj 3463)

    题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...

  10. IPv6地址介绍

    IPv6地址介绍 2008 年 04 月 10 日 1. 认识IPv6地址 IPv4地址是类似 A.B.C.D 的格式,它是32位,用\".\"分成四段,用10进制表示:而IPv6 ...