android dialog屏蔽back返回键的解决方法

需求:activity弹出dialog,按下返回键dialog消失、activity也要finish

解决方法:

直接上代码:


myMaterDialog = new MyMaterialDialog(UserBuyActivity.this);
myMaterDialog.setTitle(title);
myMaterDialog.setMessage(message);
myMaterDialog.setPositiveButton(positive, new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        myMaterDialog.dismiss();
            }
});
myMaterDialog.setNegativeButton(negative, new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        myMaterDialog.dismiss();
                    }
});
myMaterDialog.setOnListener(new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
        {
            myMaterDialog.dismiss();
            UserBuyActivity.this.finish();
        }
        return false;
    }
});

myMaterDialog.setCanceledOnTouchOutside(false);
myMaterDialog.show()
;


-----------------实现绿色背景的代码即可------------------------



你可能感兴趣的:(android,dialog屏蔽back返回键)