Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> popupwindow不響應back鍵

popupwindow不響應back鍵

編輯:關於Android編程

在popupwindwo源碼中       private void preparePopup(WindowManager.LayoutParams p) {         if (mContentView == null || mContext == null || mWindowManager == null) {             throw new IllegalStateException("You must specify a valid content view by "                     + "calling setContentView() before attempting to show the popup.");         }             if (mBackground != null) {             final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();             int height = ViewGroup.LayoutParams.MATCH_PARENT;             if (layoutParams != null &&                     layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {                 height = ViewGroup.LayoutParams.WRAP_CONTENT;             }                 // when a background is available, we embed the content view             // within another view that owns the background drawable             PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);             PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(                     ViewGroup.LayoutParams.MATCH_PARENT, height             );             popupViewContainer.setBackgroundDrawable(mBackground);             popupViewContainer.addView(mContentView, listParams);                 mPopupView = popupViewContainer;         } else {             mPopupView = mContentView;         }         mPopupWidth = p.width;         mPopupHeight = p.height;     } 當背景不為空時,才會創建PopupViewContainer ,popupwindow正式通過PopupViewContainer 來相應按鍵的。如dispatchkeyevent        private class PopupViewContainer extends FrameLayout {         private static final String TAG = "PopupWindow.PopupViewContainer";             public PopupViewContainer(Context context) {             super(context);         }             @Override         protected int[] onCreateDrawableState(int extraSpace) {             if (mAboveAnchor) {                 // 1 more needed for the above anchor state                 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);                 View.mergeDrawableStates(drawableState, ABOVE_ANCHOR_STATE_SET);                 return drawableState;             } else {                 return super.onCreateDrawableState(extraSpace);             }         }             @Override         public boolean dispatchKeyEvent(KeyEvent event) {             if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {                 if (getKeyDispatcherState() == null) {                     return super.dispatchKeyEvent(event);                 }                     if (event.getAction() == KeyEvent.ACTION_DOWN                         && event.getRepeatCount() == 0) {                     KeyEvent.DispatcherState state = getKeyDispatcherState();                     if (state != null) {                         state.startTracking(event, this);                     }                     return true;                 } else if (event.getAction() == KeyEvent.ACTION_UP) {                     KeyEvent.DispatcherState state = getKeyDispatcherState();                     if (state != null && state.isTracking(event) && !event.isCanceled()) {                         dismiss();                         return true;                     }                 }                 return super.dispatchKeyEvent(event);             } else {                 return super.dispatchKeyEvent(event);             }         } 。。。   }     綜上:若想popupwindow不響應back鍵,直接設置其background為null即可。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved