Android TabActivity中onKeyDown无效问题

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        
        //按下键盘上返回按钮
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            new AlertDialog.Builder(LoginActivity.this).setTitle("提示")
                    .setMessage("确认退出吗?")
                    .setIcon(R.drawable.icon)
                    .setPositiveButton("确定",
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which)
                                {
                                    android.os.Process.killProcess(android.os.Process.myPid()); // 结束进程
                                }
                            })
                    .setNegativeButton("取消",
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which)
                                {
                                    
                                }
                            })
                    .show();
            return true;
        }
        else
        {
            return super.onKeyDown(keyCode, event);
        }
    }

此时在 TabActivity无法监听keyevent,onKeyDown冲突了。

解决办法:

1.当继承TabActivity时,同学们是不是onKeyDown方法没用,那是应为冲突了,可以用dispatchKeyEvent方法

 @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0&&event.getAction() == KeyEvent.ACTION_DOWN )        {
            new AlertDialog.Builder(MainActivity.this).setTitle("提示")
                    .setMessage("确认退出吗?")
                    .setIcon(R.drawable.icon)
                    .setPositiveButton("确定",
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which)
                                {
                                    new RequestCommandService().logoutReq();
                                    android.os.Process.killProcess(android.os.Process.myPid()); // 结束进程
                                }
                            })
                    .setNegativeButton("取消",
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which)
                                {
                                    
                                }
                            })
                    .show();
            return false;
        }
       
            return super.dispatchKeyEvent(event);
        
    }

可以解决问题,能监听到keyEvent事件,但会监听二次,弹出二个AlertDialog。

因此在if判断需要添加event.getAction() == KeyEvent.ACTION_DOWN就OK了

2.也可以到TabActivity的子Activity用onKeyDown

3.当有双重TabActivity时可以用

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
             TouchListenerUtils.Back(this.getParent().getParent());//getParent().是指父类
          return false;      
        }else{
            return super.onKeyDown(keyCode, event);
        }
    }

你可能感兴趣的:(TabActivity)