展会信息港展会大全

popupwindow不响应back键
来源:互联网   发布日期:2015-10-13 07:31:21   浏览:1708次  

导读:在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 setC...

在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即可。

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港