如何关闭自定义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();
}
}
});
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();
}
}
});
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询