menu key 按下后

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.d("gxqtest", "enter onCreateOptionsMenu");
        menu.add("hi I am menu one");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        Log.d("gxqtest", "enter onPrepareOptionsMenu");
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        Log.d("gxqtest", "enter onMenuOpened");
        return super.onMenuOpened(featureId, menu);
    }

    @Override
    public void onOptionsMenuClosed(Menu menu) {
        Log.d("gxqtest", "enter onOptionsMenuClosed");
        super.onOptionsMenuClosed(menu);
    }


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  /**
     * Initialize the contents of the Activity's standard options menu.  You
     * should place your menu items in to <var>menu</var>.
     * 
     * <p>This is only called once, the first time the options menu is
     * displayed.  
To update the menu every time it is displayed, see
     * {@link #onPrepareOptionsMenu}.
     * 
     * <p>The default implementation populates the menu with standard system
     * menu items.  These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that 
     * they will be correctly ordered with application-defined menu items. 
     * Deriving classes should always call through to the base implementation. 
     * 
     * <p>You can safely hold on to <var>menu</var> (and any items created
     * from it), making modifications to it as desired, until the next
     * time onCreateOptionsMenu() is called.
     * 
     * <p>When you add items to the menu, you can implement the Activity's
     * {@link #onOptionsItemSelected} method to handle them there.
     * 
     * @param menu The options menu in which you place your items.
     * 
     * @return You must return true for the menu to be displayed;
     *         if you return false it will not be shown.

     * 
     * @see #onPrepareOptionsMenu
     * @see #onOptionsItemSelected
     */
    public boolean onCreateOptionsMenu(Menu menu) {
        if (mParent != null) {
            return mParent.onCreateOptionsMenu(menu);
        }
        return true;
    }


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    /**
     * Prepare the Screen's standard options menu to be displayed.  This is
     * called right before the menu is shown, every time it is shown.  You can
     * use this method to efficiently enable/disable items or otherwise
     * dynamically modify the contents.
     * 
     * <p>The default implementation updates the system menu items based on the
     * activity's state.  Deriving classes should always call through to the
     * base class implementation.
     * 
     * @param menu The options menu as last shown or first initialized by
     *             onCreateOptionsMenu().
     * 
     * @return You must return true for the menu to be displayed;
     *         if you return false it will not be shown.

     * 
     * @see #onCreateOptionsMenu
     */
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (mParent != null) {
            return mParent.onPrepareOptionsMenu(menu);
        }
        return true;
    }


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 

 

  /**
     * {@inheritDoc}
     * 
     * @return The default implementation returns true.
     */
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (featureId == Window.FEATURE_ACTION_BAR) {
            initActionBar();
            if (mActionBar != null) {
                mActionBar.dispatchMenuVisibilityChanged(true);
            } else {
                Log.e(TAG, "Tried to open action bar menu with no action bar");
            }
        }
        return true;
    }

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 

关于popupwindow替代传统menu的问题,

1.popupwindow弹出后,按menu key消失

要解决这个问题很简单,就是给PopupWindow的子View设置下面的代码:

[java]  view plain copy
  1. //sub_view 是PopupWindow的子View  
  2. sub_view.setFocusableInTouchMode(true);  
  3. sub_view.setOnKeyListener(new OnKeyListener() {  
  4.     @Override  
  5.     public boolean onKey(View v, int keyCode, KeyEvent event) {  
  6.         // TODO Auto-generated method stub  
  7.         if ((keyCode == KeyEvent.KEYCODE_MENU)&&(mPopupWindow.isShowing())) {  
  8.             mPopupWindow.dismiss();// 这里写明模拟menu的PopupWindow退出就行  
  9.             return true;  
  10.         }  
  11.         return false;  
  12.     }  
  13. });  
2. 在activity中重写onKeyDown 监控menu key,容易双重触发,从而导致 popupwindow无法显示;采用

  1.   /*必须重写,否则点击MENU无反应  为了让他不显示,下面onMenuOpened()必须返回false*/  
  2. @Override  
  3. public boolean onCreateOptionsMenu(Menu menu) {  
  4.     menu.add("menu");// 必须创建一项  
  5.     return super.onCreateOptionsMenu(menu);  
  6. }  
  7. /** 
  8.  * 拦截MENU 
  9.  */  
  10. @Override  
  11. public boolean onMenuOpened(int featureId, Menu menu) {  
  12.     if(mPopupWindow != null){  
  13.         if(!mPopupWindow.isShowing()){  
  14.             /*最重要的一步:弹出显示   在指定的位置(parent)  最后两个参数 是相对于 x / y 轴的坐标*/  
  15.             mPopupWindow.showAtLocation(findViewById(R.id.linear_menu_parent), Gravity.BOTTOM, 00);  
  16.         }  
  17.     }  
  18.     return false;// 返回为true 则显示系统menu  
  19. }  
  20.   

可解决这个问题。


可参考 http://blog.csdn.net/admin_/article/details/7278402

你可能感兴趣的:(menu key 按下后)