安卓:直到选择创建禁用OK按钮单项选择对话框 [英] Android: Create a single choice dialog with disabled OK button until selected
问题描述
我想创建一个简单的单一选择项目对话框。默认情况下,没有项目被选中。我只想要一个确定和取消按钮。直到选定一个项目的确定按钮必须保持禁用。是否有这样做的一些内置的方式或者说我要创建自己的自定义对话框?这是目前我有:
I want to create dialog with simple single choice items. By default, no items are selected. I only want an OK and Cancel button. The OK button must remain disabled until an item is selected. Is there some built-in way of doing this or do I have to create my own custom dialog? This is currently what I have:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.lbl_MarkReviewAs))
.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
selectedReviewStatusIndex = item;
AlertDialog alertDialog = (AlertDialog)dialog;
alertDialog.getButton(0).setEnabled(true);
}
})
.setPositiveButton(getString(R.string.lbl_ButtonOK), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
}
})
.setNegativeButton(getString(R.string.lbl_ButtonCancel), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
dialog.show();
这里的问题是,dialog.getButton(AlertDialog.BUTTON_POSITIVE)返回null。所以,我怎么访问的积极按钮?
The problem here is that dialog.getButton(AlertDialog.BUTTON_POSITIVE) returns null. So how do I access the positive button?
推荐答案
设置监听器选择的项目,并启用确定按钮
set listener to the Item selected and enable the "OK" Button
这篇关于安卓:直到选择创建禁用OK按钮单项选择对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!