使用复选框在Android的AlertDialog [英] AlertDialog with checkbox In android

查看:706
本文介绍了使用复选框在Android的AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid。我工作的一个应用程序中,我需要显示一个对话框,用户可以选择多个项目。为此,我想一个复选框添加到每个项目在对话框中。

I am new to Android. I am working on an app in which I need to show a dialog in which user can select more than one item. For this I want to add a checkbox to each item in the dialog.

我看了下帖子的计算器:

I read following posts on StackOverflow:

如何添加一个复选框警报对话

Android的复选框对话框(简易)

其中一个职位描述如何创建XML定义除复选框以膨胀的对话框中,但我无法得到它的工作。

One of these posts describe how to create XML defining the checkbox in addition to inflating the dialog, but I could not get it to work.

推荐答案

添加复选框AlertDialog很好在博客中以实例 AlertDialog使用复选框在Android中

您可以访问博客 AlertDialog使用复选框在Android中 了解更多的细节,并从相同的博客以下

final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
// arraylist to keep the selected items
final ArrayList seletedItems=new ArrayList();

AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Select The Difficulty Level")
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
        if (isChecked) {
            // If the user checked the item, add it to the selected items
            seletedItems.add(indexSelected);
        } else if (seletedItems.contains(indexSelected)) {
            // Else, if the item is already in the array, remove it
            seletedItems.remove(Integer.valueOf(indexSelected));
        }
    }
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        //  Your code when user clicked on OK
        //  You can write the code  to save the selected item here
    }
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        //  Your code when user clicked on Cancel
    }
}).create();
dialog.show();

这篇关于使用复选框在Android的AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆