作者:超级神棍小虾米 | 来源:互联网 | 2023-10-10 11:43
//转:
原文地址
AlertDialog.Builder builder = new Builder(this);
builder.create().show();
这样显示出来的对话框,当用户按返回键或搜索键时,这个对话框也能关闭。
实现代码:
AlertDialog.Builder builder = new Builder(this);
builder.setMessage(msg);
builder.setTitle(title);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.setCancelable(false);//设置这个对话框不能被用户按[返回键]而取消掉,但如果用户按了KeyEvent.KEYCODE_SEARCH,对话框还是会Dismiss掉,这里的setOnKeyListener作用就是屏蔽用户按下KeyEvent.KEYCODE_SEARCH
alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
return (keyCode == KeyEvent.KEYCODE_SEARCH);
}
});
alertDialog.show();