AlertDialog与复选框在android

AlertDialog与复选框在android

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

问题描述

我是Android的新手。我正在开发一个应用程序,其中我需要显示一个对话框,用户可以在其中选择多个项目。为此,我想在对话框中的每个项目中添加一个复选框。



我在StackOverflow上阅读了以下帖子:







其中一个帖子描述了如何创建定义复选框的XML,除了夸大对话框,但我无法使其工作。 / p>

解决方案

添加在AlertDialog中的复选框在博客中有很好的解释,例如



您可以访问博客 有关详细信息,以下内容来自同一个博客

  final CharSequence [] items = {Easy,Medium,Hard,Very Hard}; 
//数组列表保留所选项目
final ArrayList seletedItems = new ArrayList();

AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(选择难度级别)
.setMultiChoiceItems(items,null,new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog,int indexSelected,boolean isChecked){
if(isChecked){
//如果用户检查了项目,将其添加到选择的项目
seletedItems.add(indexSelected);
} else if(seletedItems.contains(indexSelected)){
//否则,如果项目已经在数组中,删除它
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
})。setPositiveButton(OK,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface对话框,int id){
//用户点击OK后,您的代码
//您可以编写代码以保存所选项目
}
}) tNegativeButton(取消,新的DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int id){
//您的代码,当用户点击取消
}
})。create();
dialog.show();


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:

How to add a check box to an alert dialog and

Android Checkbox Dialog (Easy)

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.

解决方案

Adding checkbox in AlertDialog is well explained in the blog with example AlertDialog With CheckBox In Android

You can visit the blog AlertDialog With CheckBox In Android for more detail, and the following is taken from the same blog

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();

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

08-31 01:33