我的NavActivity实现TabActivity并有五个子选项卡。我想在客户按下设备上的按钮时显示警告对话框。但由于标题令人伤心,它总是在这种情况下没有回应。
答案 0 :(得分:1)
标题上写着“onKeyDown”,但你不需要点击“后退”按钮。只需覆盖您活动的onBackPressed()
方法,如下所示:
@Override
public void onBackPressed(){
AlertDialog ad = new AlertDialog.Builder(this)
.setMessage("Proceed with back pressed?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
...
dialog.dismiss();
}
})
.create();
ad.show();
}
答案 1 :(得分:0)
调用子活动的onKeyDown方法。为了调用TabActity的onKeyDown,只需调用子活动中的超级onKeyDown即可 return this.getParent()。onKeyDown(keyCode,event);
以下是您的代码: 在儿童活动中:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return this.getParent().onKeyDown(keyCode, event);
}
在你的TabActivity中是这样的:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
if(currenttab==0)
{
Log.e("current tab if", tabHost.getCurrentTab()+"");
finish();
}
else
{
Log.e("current tab else",tabHost.getCurrentTab()+"");
tabHost.setCurrentTab(0);
return true; //Make sure to call return true else the onkeyDown event will be propogated to the parent method.
}
}
return false;
}