android常用对话框封装
在android开发中,经常会用到对话框跟用户进行交互,方便用户可操作性;接下来就对常用对话框进行简单封装,避免在项目中出现冗余代码,加重后期项目的维护量;代码如有问题欢迎大家拍砖指正一起进步。
先贴出演示结果,在晒出演示代码。
1、运行成功后,原始界面如下:
2、点击“显示普通对话框”,效果界面如下:
3、点击“显示列表对话框”,效果界面如下:
4、点击“显示单选按钮对话框”,效果界面如下:
5、点击“显示复选对话框”,效果界面如下:
代码:
1、项目目录结构如下
2、对话框封装类DialogTool
- package com.hrtx.util;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface.OnClickListener;
- /**
- * 对话框封装类
- *
- * @author jiqinlin
- *
- */
- public class DialogTool {
- /**
- * 创建普通对话框
- *
- * @param ctx 上下文 必填
- * @param iconId 图标,如:R.drawable.icon 必填
- * @param title 标题 必填
- * @param message 显示内容 必填
- * @param btnName 按钮名称 必填
- * @param listener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
- * @return
- */
- public static Dialog createNormalDialog(Context ctx,
- int iconId,
- String title,
- String message,
- String btnName,
- OnClickListener listener) {
- Dialog dialog=null;
- android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
- // 设置对话框的图标
- builder.setIcon(iconId);
- // 设置对话框的标题
- builder.setTitle(title);
- // 设置对话框的显示内容
- builder.setMessage(message);
- // 添加按钮,android.content.DialogInterface.OnClickListener.OnClickListener
- builder.setPositiveButton(btnName, listener);
- // 创建一个普通对话框
- dialog = builder.create();
- return dialog;
- }
- /**
- * 创建列表对话框
- *
- * @param ctx 上下文 必填
- * @param iconId 图标,如:R.drawable.icon 必填
- * @param title 标题 必填
- * @param itemsId 字符串数组资源id 必填
- * @param listener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
- * @return
- */
- public static Dialog createListDialog(Context ctx,
- int iconId,
- String title,
- int itemsId,
- OnClickListener listener) {
- Dialog dialog=null;
- android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
- // 设置对话框的图标
- builder.setIcon(iconId);
- // 设置对话框的标题
- builder.setTitle(title);
- // 添加按钮,android.content.DialogInterface.OnClickListener.OnClickListener
- builder.setItems(itemsId, listener);
- // 创建一个列表对话框
- dialog = builder.create();
- return dialog;
- }
- /**
- * 创建单选按钮对话框
- *
- * @param ctx 上下文 必填
- * @param iconId 图标,如:R.drawable.icon 必填
- * @param title 标题 必填
- * @param itemsId 字符串数组资源id 必填
- * @param listener 单选按钮项监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
- * @param btnName 按钮名称 必填
- * @param listener2 按钮监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
- * @return
- */
- public static Dialog createRadioDialog(Context ctx,
- int iconId,
- String title,
- int itemsId,
- OnClickListener listener,
- String btnName,
- OnClickListener listener2) {
- Dialog dialog=null;
- android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
- // 设置对话框的图标
- builder.setIcon(iconId);
- // 设置对话框的标题
- builder.setTitle(title);
- // 0: 默认第一个单选按钮被选中
- builder.setSingleChoiceItems(itemsId, 0, listener);
- // 添加一个按钮
- builder.setPositiveButton(btnName, listener2) ;
- // 创建一个单选按钮对话框
- dialog = builder.create();
- return dialog;
- }
- /**
- * 创建复选对话框
- *
- * @param ctx 上下文 必填
- * @param iconId 图标,如:R.drawable.icon 必填
- * @param title 标题 必填
- * @param itemsId 字符串数组资源id 必填
- * @param flags 初始复选情况 必填
- * @param listener 单选按钮项监听器,需实现android.content.DialogInterface.OnMultiChoiceClickListener接口 必填
- * @param btnName 按钮名称 必填
- * @param listener2 按钮监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
- * @return
- */
- public static Dialog createCheckBoxDialog(Context ctx,
- int iconId,
- String title,
- int itemsId,
- boolean[] flags,
- android.content.DialogInterface.OnMultiChoiceClickListener listener,
- String btnName,
- OnClickListener listener2) {
- Dialog dialog=null;
- android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
- // 设置对话框的图标
- builder.setIcon(iconId);
- // 设置对话框的标题
- builder.setTitle(title);
- builder.setMultiChoiceItems(itemsId, flags, listener);
- // 添加一个按钮
- builder.setPositiveButton(btnName, listener2) ;
- // 创建一个复选对话框
- dialog = builder.create();
- return dialog;
- }
- }
3、对话框Activity类DialogActivity
- package com.ljq.activity;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * 对话框Activity类
- *
- * @author jiqinlin
- *
- */
- public class DialogActivity extends Activity {
- private boolean[] flags=new boolean[]{false,true,false}; //初始复选情况
- private String[] items=null;
- private EditText etText=null;
- private Button btnNormal=null;
- private Button btnList=null;
- private Button btnRadio=null;
- private Button btnCheckBox=null;
- private static final int DIALOG_NORMAL=0; //普通对话框常量
- private static final int DIALOG_LIST=1; //列表对话框常量
- private static final int DIALOG_RADIO=2; //单选按钮对话框常量
- private static final int DIALOG_CHECKBOX=3; //复选对话框常量
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- items=getResources().getStringArray(R.array.hobby);
- etText=(EditText)findViewById(R.id.etText);
- btnNormal=(Button)findViewById(R.id.btnNormal);
- btnList=(Button)findViewById(R.id.btnList);
- btnRadio=(Button)findViewById(R.id.btnRadio);
- btnCheckBox=(Button)findViewById(R.id.btnCheckBox);
- btnNormal.setOnClickListener(l);
- btnList.setOnClickListener(l);
- btnRadio.setOnClickListener(l);
- btnCheckBox.setOnClickListener(l);
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- Dialog dialog=null;
- switch (id) {
- case DIALOG_NORMAL: //创建普通对话框
- dialog = DialogTool.createNormalDialog(this,
- R.drawable.icon,
- "普通对话框",
- "这是普通对话框中的内容!",
- " 确 定 ",
- new android.content.DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- etText.setText("这是普通对话框中的内容!");
- return;
- }
- }
- );
- break;
- case DIALOG_LIST: // 创建列表对话框
- dialog = DialogTool.createListDialog(this,
- R.drawable.icon,
- "列表对话框",
- R.array.hobby,
- new android.content.DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- String hoddy=getResources().getStringArray(R.array.hobby)[which];
- etText.setText("您选择了: "+hoddy);
- return;
- }
- }
- );
- break;
- case DIALOG_RADIO: // 创建单选按钮对话框
- dialog=DialogTool.createRadioDialog(this,
- R.drawable.icon,
- "单选按钮对话框",
- R.array.hobby,
- new android.content.DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- String hoddy = getResources().getStringArray(
- R.array.hobby)[which];
- etText.setText("您选择了: " + hoddy);
- return;
- }
- },
- " 确 定 ",
- new android.content.DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DialogActivity.this,
- "您按了确定按钮!", Toast.LENGTH_LONG).show();
- return;
- }
- }
- );
- break;
- case DIALOG_CHECKBOX: // 创建复选框对话框
- dialog=DialogTool.createCheckBoxDialog(this,
- R.drawable.icon,
- "复选对话框",
- R.array.hobby,
- flags,
- new DialogInterface.OnMultiChoiceClickListener() {
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- flags[which] = isChecked;
- String result = "您选择了:";
- for (int i = 0; i < flags.length; i++) {
- if (flags[i]) {
- result = result + items[i] + "、";
- }
- }
- etText.setText(result.substring(0, result.length() - 1));
- }
- },
- " 确 认 ",
- new android.content.DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DialogActivity.this, "您按了确定按钮!", Toast.LENGTH_LONG).show();
- return;
- }
- }
- );
- break;
- }
- return dialog;
- }
- //按钮监听
- View.OnClickListener l = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Button btn = (Button) v;
- switch (btn.getId()) {
- case R.id.btnNormal: //普通对话框
- //显示对话框
- showDialog(DIALOG_NORMAL);
- break;
- case R.id.btnList: //列表对话框
- //显示对话框
- showDialog(DIALOG_LIST);
- break;
- case R.id.btnRadio: //单选按钮对话框
- //显示对话框
- showDialog(DIALOG_RADIO);
- break;
- case R.id.btnCheckBox: //复选对话框
- //显示对话框
- showDialog(DIALOG_CHECKBOX);
- break;
- }
- }
- };
- }
4、布局文件main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <EditText android:text=""
- android:id="@+id/etText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:editable="false"
- android:cursorVisible="false" />
- <Button android:text="显示普通对话框"
- android:id="@+id/btnNormal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <Button android:text="显示列表话框"
- android:id="@+id/btnList"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <Button android:text="显示单选按钮对话框"
- android:id="@+id/btnRadio"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <Button android:text="显示复选对话框"
- android:id="@+id/btnCheckBox"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
5、数组变量array.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- resources的使用
- resoureces就是res目录下的那些目录和文件,常用的有:
- res/drawable/ 用来存放图片文件
- res/layout/ 用来存放布局定义文件
- res/values/ 用来存放一些变量、参数等文件
- -->
- <resources>
- <string-array name="hobby">
- <item>游泳</item>
- <item>打篮球</item>
- <item>登山</item>
- </string-array>
- </resources>
6、清单文件
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.ljq.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".DialogActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="4" />
- </manifest>
完毕!!!!!
android常用对话框封装的更多相关文章
- Android 常用对话框Dialog封装
Android 6种 常用对话框Dialog封装 包括: 消息对话框.警示(含确认.取消)对话框.单选对话框. 复选对话框.列表对话框.自定义视图(含确认.取消)对话框 分别如下图所示: ...
- (转载)Android常用的Dialog对话框用法
Android常用的Dialog对话框用法 Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的 ...
- Android常用酷炫控件(开源项目)github地址汇总
转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...
- Android 常用炫酷控件(开源项目)git地址汇总
第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...
- Android常用组件
UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...
- Android常用组件【转】
UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...
- android sqlite数据库封装 实现crud
android常用的数据保存方式有文件.sharepreferences.数据库.网络.contentprovider集中方式. 文件存储方式,经常使用在缓存整个页面数据,比如电子书内容.html数据 ...
- Android 常用 adb 命令总结
Android 常用 adb 命令总结 针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb 命令熟记于心, 将会为 Android 测试带来很大的方便,其中很多命 ...
- Android 常用代码大集合 [转]
[Android]调用字符串资源的几种方法 字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...
随机推荐
- Js注释
注释 介绍 作用 合作分享:方便他人阅读,便于分享 沉淀总结:容易忘记代码,自己总结沉淀 形式 1.// 双斜杠 2./**/斜杠星号 常用标签 标签 描述 @module 标明当前文件模块,在这个文 ...
- Ubuntu ( Linux) Eclipse 乱码问题
刚装完Ubuntu,导入Java和Android项目时,发现字符乱码,究其原因,是由于Windows下使用的是GBK编码,而Ubuntu使用的是UTF-8编码.网上查找了相关资料,主要解决方案有两种. ...
- Linux内存管理原理
本文以32位机器为准,串讲一些内存管理的知识点. 1. 虚拟地址.物理地址.逻辑地址.线性地址 虚拟地址又叫线性地址.linux没有采用分段机制,所以逻辑地址和虚拟地址(线性地址)(在用户态,内核态逻 ...
- js的json转换
静态页面是: data:[{ value:2.5, itemStyle:{ normal:{color:'#4a90e2'} } },{ value:2.5, itemStyle:{ normal:{ ...
- JS不用通过其他转换两个小数加减得到正确答案
之前写过一篇文章js比较两个属于float类型的小数,都需要通过某种函数转换下,太麻烦了,比如: 减法:10.2345-0.01=10.2245,这是正确的答案,但是当你做加法的时候就变了 加法:10 ...
- jquery this 与javascript的this
<div class="list"> <table> <thead> <tr> <th width="110&quo ...
- CentOS 7.0 部署 Django 到运行起来第一个web service
最近在学习Python,今天发现Django如此强大的web框架,不得不来试一试. 1. 安装Python,官网建议用Python3:
- xmpp的bug
[微分享]:事前必三思,事中要坚韧,事后莫悔恨,只有眼光看远些,脚步坚实些,人生方多些圆满,少些遗憾. xmpp的bug
- Sightseeing(poj 3463)
题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...
- IPv6地址介绍
IPv6地址介绍 2008 年 04 月 10 日 1. 认识IPv6地址 IPv4地址是类似 A.B.C.D 的格式,它是32位,用\".\"分成四段,用10进制表示:而IPv6 ...