见博客文章
1)给对话框加监听,代码如下:
(2)捕捉back键,重写dispatchKeyEvent()方法,注意不是onKeyDown()方法。代码如下:
通过对话框主动监听back键,这样才能避免对话框存在时无法捕获back键。
http://blog.csdn.net/wangpp2009/article/details/7106906
1)给对话框加监听,代码如下:
dialog.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
{
dialog.dismiss();
}
return false;
}
});
(2)捕捉back键,重写dispatchKeyEvent()方法,注意不是onKeyDown()方法。代码如下:
public boolean dispatchKeyEvent(KeyEvent event)
{
switch(event.getKeyCode())
{
case KeyEvent.KEYCODE_BACK:
clearData();
break;
default:
break;
}
return super.dispatchKeyEvent(event);
}
通过对话框主动监听back键,这样才能避免对话框存在时无法捕获back键。