为什么popUpMenu不会在KeyEvent上显示?

时间:2012-04-02 15:54:18

标签: java android popupmenu

我有一个全屏的视频视图,并且想要在按下某个键时触发popupMenu。日志显示捕获了键事件,但弹出视图不会显示。有什么想法吗?

 @Override
  public boolean onKeyDown(int keyCode , KeyEvent event ){
      switch(keyCode){
        case KeyEvent.KEYCODE_DPAD_CENTER:
            Log.i("POP uP MENU","Show");
            mVideoView.pause();
            showPopupMenu(new View(VideoPlayer.this));


        case KeyEvent.KEYCODE_D:
            popupMenu.dismiss();


      }
      return true;
  }


private void showPopupMenu(View v){
       popupMenu = new PopupMenu(VideoPlayer.this, v);


   if(bitRates != null){
          for(int i=0;i<bitRates.size();i++){
              int menuItem = i;
              popupMenu.getMenu().add(0,menuItem, 0,qualityList.get(i)+" : "+bitRates.get(i));

          }
      }
  popupMenu.show(); 
  popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

   public boolean onMenuItemClick(final MenuItem item) {
       progressDialog = ProgressDialog.show(VideoPlayer.this, "", "Loading video...");

       final int position = mVideoView.getCurrentPosition();
       new Thread() {

            public void run() {

                try{


                    runOnUiThread(new Runnable() {
                        public void run() {
                            play(streamUrls.get(item.getItemId()),position); 
                        }
                    });


                }
                catch (Exception e) 
                {

                    Log.e("tag", e.getMessage());

                }
                progressDialog.dismiss();
            }

        }.start();

       return true;

   }
  });

}

1 个答案:

答案 0 :(得分:0)

似乎

KeyEvent.KEYCODE_DPAD_CENTER

是默认情况下设置为由 VideoView 触发的事件。因此,当我按下该键时,VideoView触发了它的事件事件,而不是我请求它的事件。我将KeyEvent KEYCODE更改为另一个键(一个未被Videoview使用)并且它可以工作。