Chr*_*rys 4 android android-alertdialog onclicklistener
我正在使用AlertDialog.Builder来构建我的对话框,它有一个需要填充的EditText,我想阻止关闭对话框,而不是.在正面按钮的onClickListener中,我可以检查editText是否已填充,但我不知道如何阻止关闭...
builder.setPositiveButton("title", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if(...){
//can close
}else{
//prevent closing
}
}
})
Run Code Online (Sandbox Code Playgroud)
Ant*_*met 30
您可以在调用show()对话框后立即更改按钮的行为,如下所示.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test for preventing dialog close");
builder.setPositiveButton("Test",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do nothing here because we override this button later to change the close behaviour.
//However, we still need this because on older versions of Android unless we
//pass a handler the button doesn't get instantiated
}
});
AlertDialog dialog = builder.create();
dialog.show();
//Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Boolean wantToCloseDialog = false;
//Do stuff, possibly set wantToCloseDialog to true then...
if(wantToCloseDialog)
dialog.dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15005 次 |
| 最近记录: |