首页 > 代码库 > AlertDialog自定义关闭
AlertDialog自定义关闭
如果AlertDialog里有输入框,那么如何根据自己的需要关闭AlertDialog,网上有很多是通过反射方式。这里有另外一种方法。
Builder builder = new Builder(MainActivity.this);
builder.setTitle("服务器设置");
LayoutInflater inflater = LayoutInflater.from(this);
final View view = inflater.inflate(R.layout.edit, null);
builder.setView(view);
builder.setPositiveButton(R.string.ok, new AlertDialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog dialog = builder.create();
//builder.show();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Boolean wantToCloseDialog = false;
//需要时修改wantToCloseDialog的值
if(wantToCloseDialog){
dialog.dismiss();
}else{
Toast.makeText(MainActivity.this, "服务器地址不符合规范", Toast.LENGTH_SHORT).show();
}
}
});