先放效果截图

项目中需要有个Dialog全选对话框,点击全选全部选中,取消全选全部取消。下午查了些资料,重写了一下Dialog对话框。把代码放出来。

public class MainActivity extends Activity {
View getlistview;
String[] mlistText = { "全选", "选择1", "选择2", "选择3", "选择4", "选择5", "选择6", "选择7" };
ArrayList<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
AlertDialog.Builder builder;
AlertDialog builder2;
SimpleAdapter adapter;
Boolean[] bl = { false, false, false, false, false, false, false, false }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn);
int lengh = mlistText.length;
for (int i = 0; i < lengh; i++) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("text", mlistText[i]);
mData.add(item);
}
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CreateDialog();// 点击创建Dialog
}
}); } class ItemOnClick implements OnItemClickListener { @Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
CheckBox cBox = (CheckBox) view.findViewById(R.id.X_checkbox);
if (cBox.isChecked()) {
cBox.setChecked(false);
} else {
Log.i("TAG", "取消该选项");
cBox.setChecked(true);
} if (position == 0 && (cBox.isChecked())) {
//如果是选中 全选 就把所有的都选上 然后更新
for (int i = 0; i < bl.length; i++) {
bl[i] = true;
}
adapter.notifyDataSetChanged();
} else if (position == 0 && (!cBox.isChecked())) {
//如果是取消全选 就把所有的都取消 然后更新
for (int i = 0; i < bl.length; i++) {
bl[i] = false;
}
adapter.notifyDataSetChanged();
}
if (position != 0 && (!cBox.isChecked())) {
// 如果把其它的选项取消 把全选取消
bl[0] = false;
bl[position]=false;
adapter.notifyDataSetChanged();
} else if (position != 0 && (cBox.isChecked())) {
//如果选择其它的选项,看是否全部选择
//先把该选项选中 设置为true
bl[position]=true;
int a = 0;
for (int i = 1; i < bl.length; i++) {
if (bl[i] == false) {
//如果有一个没选中 就不是全选 直接跳出循环
break;
} else {
//计算有多少个选中的
a++;
if (a == bl.length - 1) {
//如果选项都选中,就把全选 选中,然后更新
bl[0] = true;
adapter.notifyDataSetChanged();
}
}
}
}
} } public void CreateDialog() { // 动态加载一个listview的布局文件进来
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
getlistview = inflater.inflate(R.layout.listview, null); // 给ListView绑定内容
ListView listview = (ListView) getlistview.findViewById(R.id.X_listview);
adapter = new SetSimpleAdapter(MainActivity.this, mData, R.layout.listitem, new String[] { "text" },
new int[] { R.id.X_item_text });
// 给listview加入适配器
listview.setAdapter(adapter);
listview.setItemsCanFocus(false);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listview.setOnItemClickListener(new ItemOnClick()); builder = new AlertDialog.Builder(this);
builder.setTitle("请选择查询类型");
builder.setIcon(R.drawable.ic_launcher);
//设置加载的listview
builder.setView(getlistview);
builder.setPositiveButton("确定", new DialogOnClick());
builder.setNegativeButton("取消", new DialogOnClick());
builder.create().show();
} class DialogOnClick implements DialogInterface.OnClickListener { @Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case Dialog.BUTTON_POSITIVE:
//确定按钮的事件
break;
case Dialog.BUTTON_NEGATIVE:
//取消按钮的事件
break;
default:
break;
}
}
} //重写simpleadapterd的getview方法
class SetSimpleAdapter extends SimpleAdapter { public SetSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LinearLayout.inflate(getBaseContext(), R.layout.listitem, null);
}
CheckBox ckBox = (CheckBox) convertView.findViewById(R.id.X_checkbox);
//每次都根据 bl[]来更新checkbox
if (bl[position] == true) {
ckBox.setChecked(true);
} else if (bl[position] == false) {
ckBox.setChecked(false);
}
return super.getView(position, convertView, parent);
}
}
}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击弹出Dialog" /> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <ListView
android:id="@+id/X_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants" > <TextView
android:id="@+id/X_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项" /> <CheckBox
android:id="@+id/X_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="false"
android:focusable="false" />
</RelativeLayout> </LinearLayout>

Android自定义Dialog多选对话框(Dialog+Listview+CheckBox)的更多相关文章

  1. Android自定义类似ProgressDialog效果的Dialog

    Android自定义类似ProgressDialog效果的Dialog. 方法如下: 1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景). 如我要的效果: 2.定义loadin ...

  2. Android自定义底部带有动画的Dialog

    Android自定义底部带有动画的Dialog 效果图 先看效果图,是不是你想要的呢 自定义Dialog package --.view; import android.app.Dialog; imp ...

  3. android 自定义view之选座功能

    效果图: 界面比较粗糙,主要看原理. 这个界面主要包括以下几部分 1.座位 2.左边的排数 3.左上方的缩略图 4.缩略图中的红色区域 5.手指移动时跟随移动 6.两个手指缩放时跟随缩放 主要技术点 ...

  4. 安卓使用Dialog创建普通对话框

    Activity页面简单所以XML不再写出.下面给出核心代码: button1=(Button)findViewById(R.id.button1); //为按钮设置监听器  button1.setO ...

  5. Android自定义遮罩层设计

    在做网页设计时,前端设计人员会经常用到基于JS开发的遮罩层,并且背景半透明.这样的效果怎么样在Android上实现呢?这个实现并不困难,先来上效果图: <ignore_js_op> 201 ...

  6. (转载)Android自定义ProgressDialog进度等待框

    Android自定义ProgressDialog进度等待框 作者:无缘公子 字体:[增加 减小] 类型:转载 时间:2016-01-11我要评论 这篇文章主要介绍了Android自定义Progress ...

  7. android自定义dialog中点击listview的item事件关闭dialog

    import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; ...

  8. Android自定义 Dialog 对话框

    Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...

  9. Android中的普通对话框、单选对话框、多选对话框、带Icon的对话框、以及自定义Adapter和自定义View对话框详解

    对话框就是一个AlertDialog,但是一个简单的AlertDialog,我们却可以将它玩出许多花样来,下面我们就来一起总结一下AlertDialog的用法.看看各位童鞋在平时的工作中否都用到了Al ...

随机推荐

  1. python3.7 random模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 random模块 import random #随机模块 # r ...

  2. Python9-MySQL数据库安装及基本操作-day42

    MySQL 单机程序(自己DB) 单机程序(共用DB)MySQL:用于管理文件的一个软件 -服务端软件 -socket服务端 -本地文件操作 -解析指令[SQL语句] -客户端软件(各种各样) -so ...

  3. POJ1426-Find The Multiple(搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42035   Accepted: 176 ...

  4. JVM——Java类加载机制总结

    )解析:解析阶段是把虚拟机中常量池的符号引用替换为直接引用的过程. 2.3 初始化 类初始化时类加载的最后一步,前面除了加载阶段用户可以通过自定义类加载器参与以外,其余都是虚拟机主导和控制.到了初始化 ...

  5. Maya建模命令

    Surface-Loft(放样)在两条曲线中间生成曲面Section Radius 改变圆环的圆环半径Edit Mesh- Merge 点连结挤压 keep face together(整体挤压),若 ...

  6. border-color与color

    1.border-color就是color,即border-color的默认颜色就是color 当没有指定border-color的时候,会使用color作为边框的颜色! 类似的还有text-shad ...

  7. java.util.ArrayList与java.util.Arrays$ArrayList区别

    本博客转载自:https://blog.csdn.net/maywehe/article/details/52553954 写demo的时候,为了避免用list.add方法,特意写了个数组然后转换成l ...

  8. loj2063 「HAOI2016」字符合并

    ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  9. 使用 SceneLoader 类在 XNA 中显示载入屏幕(十)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  10. python学习-- settings 设置sqlserver连接

    PyCharm 开发工具 先打开项目 1.  ctrl+alt+s 2. project:项目名称  选中Project Interpreter,点右面+号 :搜索  django-pyodbc-az ...