Android AlertDialog 点击区域外不可取消,点击返回键可以与 Activity 同时撤销
Android About 1,045 wordsAlertDialog 属性设置
点击区域外不可取消,点击返回键可以与Activity
同时撤销
public void initAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = View.inflate(this, R.layout.dialog_view, null);
builder.setView(view);
final AlertDialog alertDialog = builder.create();
view.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
//点击之后对话框消失,实现业务逻辑
}
});
alertDialog.setCanceledOnTouchOutside(false);//对话框区域外点击无效,但是可以点击返回键
alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {//监听返回键,与activity同时销毁,否则会先dismiss再finish
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();//要达到同时销毁效果,可在finish()后dismiss,如果不dismiss,dialog会leak。
}
return false;
}
});
alertDialog.show();
}
Views: 6,059 · Posted: 2019-04-10
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...