【问题标题】:Disable AlertDialog PositiveButton If No CheckBox is Checked如果未选中 CheckBox,则禁用 AlertDialog PositiveButton
【发布时间】:2015-11-16 20:44:28
【问题描述】:

我有以下代码:

final AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setTitle(R.string.str_dbox_remove_plant_title);
final HashMap<String, Integer> stuff = myCollection.getListOfStuffWithLocation();
final CharSequence[] items = stuff.keySet().toArray(new CharSequence[stuff.size()]);
final List<Integer> indexesOfSelectedStuff = new ArrayList<Integer>();
builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        if (isChecked)
            indexesOfSelectedStuff.add(stuff.get(items[which].toString()));
        else if (indexesOfSelectedStuff.contains(stuff.get(items[which].toString())))
            indexesOfSelectedStuff.remove(which);
    }
});
builder.setPositiveButton(R.string.str_dbox_remove_stuff_ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        for (int i = 0; i < indexesOfSelectedStuff.size(); i++) {
            myCollection.removeStuff(i);
        }
    }
});
builder.setNegativeButton(R.string.str_dbox_remove_stuff_cancel, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});              

builder.setCancelable(false); // so as to prevent the back button from closing the dialog box
final AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(false);                  
alert.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); // by default, disable OK button
alert.show();

我有一个AlertDialog 和一些CheckBoxes,我想禁用PositiveButton,只要没有选中任何CheckBoxes。

我有一个名为indexesOfSelectedStuff 的List,你可以说,在某种程度上,使用它的size() 方法可以让我知道选择了多少CheckBoxes。但是,我不确定在哪里放置它或如何在构建器中引用PositiveButton。我想把它放在onClick 方法上(在OnMultipleChoiceClickListener() 中定义),像这样:

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (isChecked)
        indexesOfSelectedStuff.add(stuff.get(items[which].toString()));
    else if (indexesOfSelectedStuff.contains(stuff.get(items[which].toString())))
        indexesOfSelectedStuff.remove(which);
    if (indexesOfSelectedStuff.size() == 0) {
        // disable button
        // but problem is, I don't know how to reference the POSITIVE_BUTTON
    }
    else {
        // enable button
    }
}

我知道我很可能无法将其插入那里。在创建AlertDialog 之后,我必须进行检查。但如果是这样的话,你会建议我使用什么?我在想Listeners 但是哪个?我应该怎么做?

【问题讨论】:

  • 我知道它与此类似:How to disable / enable dialog negative positive buttons?。所以,那里的答案有点建议我为我所有的CheckBoxes 做听众,但我真的不知道他们有多少。或者他们的身份证,甚至。因为我只是让AlertDialog.Builder 为我做这件事。

标签: android button checkbox android-alertdialog builder


【解决方案1】:

为此,您需要创建一个自定义对话框。要知道如何检查此链接。 Click Here

你可以试试这个。

创建一个布尔值作为真。 如果勾选复选框,则将布尔值设置为 true false。

然后,每当您创建警报对话框时,只需检查布尔值是否设置为 false 或 true。如果为 true,则创建一个是按钮,否则不创建一个是按钮。 现在的问题是检查按钮在 alertdialog 里面,如果是这样的话,我是对的。

每次单击复选按钮时,您都可以关闭“警报”对话框并再次打开它,其中已勾选复选框并且没有“是”按钮。那就是使用引用的按钮来设置禁用。或者用技术术语setEnabled = "false;"

当然这是滞后的,但这是我能做的。对不起

【讨论】:

  • 链接失效。无论如何,谢谢你的建议。如果找不到其他方法,我会考虑。 :)
  • 非常抱歉。请立即查看链接。
  • 哈哈。伙计,没关系。别再说“对不起”了。你让我听起来像我很刻薄。再次感谢。
  • 没问题。只是说。
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 2023-03-16
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
相关资源
最近更新 更多