码迷,mamicode.com
首页 > 移动开发 > 详细

Android TabActivity中onKeyDown无效问题

时间:2015-05-07 18:37:59      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

@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);
        }
    }

Android TabActivity中onKeyDown无效问题

标签:

原文地址:http://www.cnblogs.com/ycxyyzw/p/4485633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!