zoukankan      html  css  js  c++  java
  • 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);
            }
        }

  • 相关阅读:
    华为帐号服务助力应用运营和用户转化
    【接入指南】华为帐号服务Authorization Code模式介绍与接入步骤详解
    华为游戏登录验签失败can not find publicKey of the cp
    [古文观止]《相州昼锦堂记》(宋 欧阳修)
    [源码分析] Dynomite 分布式存储引擎 之 DynoJedisClient(2)
    [源码分析] Dynomite 分布式存储引擎 之 DynoJedisClient(1)
    Amazon Dynamo系统架构
    [从源码学设计] Flume 之 memory channel
    [阿里DIEN] 深度兴趣进化网络源码分析 之 Keras版本
    [从源码学设计]蚂蚁金服SOFARegistry之延迟操作
  • 原文地址:https://www.cnblogs.com/ycxyyzw/p/4485633.html
Copyright © 2011-2022 走看看