啥也不说了上代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d("AAAAAAA", "main onkeydown keyCode = " + keyCode + " event.getAction() = " + event.getAction());
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.mipmap.ic_launcher);
normalDialog.setTitle("我是一个普通Dialog");
normalDialog.setMessage("你要点击哪一个按钮呢?");
normalDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "嘻嘻嘻", LENGTH_SHORT).show();
}
});
normalDialog.setNegativeButton("关闭",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "guan", LENGTH_SHORT).show();
}
});
// 显示
normalDialog.show();
normalDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
Log.d("AAAAAAA", "dialog onkeydown keyCode = " + keyCode + " event.getAction() = " + event.getAction());
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialog.dismiss();
return true;
} else {
return false;
}
}
});
return true;
}
return super.onKeyDown(keyCode, event);
}
}
在Mainactivity中拦截返回键,弹出弹框,然后点返回键弹框不消失,求大哥们看看有什么解决办法。
打印出来日志显示,p20点返回键没有onkeydown事件。
环境: