当前位置:主页 > 移动开发 > Android代码 >

android popuwindow点击外部窗口不消失的实例

时间:2020-10-26 23:25:52 | 栏目:Android代码 | 点击:

网上大多数都是说如何监听点击外部消失,但我遇到的问题是点击外面不让其消失,如下是我的解决方案,关键是:popupMenu.setBackgroundDrawable(null);popupMenu.setFocusable(false);

popupMenu = new PopupWindow(view, keylinearlayout.getWidth(), LayoutParams.WRAP_CONTENT);

 ColorDrawable cd = new ColorDrawable(R.color.white);
 //popupMenu.setBackgroundDrawable(cd);
 popupMenu.setBackgroundDrawable(null);
 popupMenu.setAnimationStyle(R.style.AnimBottom);
 popupMenu.update();
 popupMenu.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
 popupMenu.setTouchable(true); // 设置popupwindow可点击
 popupMenu.setOutsideTouchable(false); // 设置popupwindow外部可点击
 popupMenu.setFocusable(false); // 获取焦点

 popupMenu.setTouchInterceptor(new View.OnTouchListener() {

  @Override
  public boolean onTouch(View v, MotionEvent event) {
  // 如果点击了popupwindow的外部,popupwindow也会消失
  // 这里如果返回true的话,touch事件将被拦截
  // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
  if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
   //popupMenu.dismiss();
   StationEventCaptain.getInstance().fireEventDataChange(
    new StationEventData(IStationEventName.HEDGE_KEYBORAD_DIMISS, true));
   return true;
  }
  return false;
  }
 });

 //初始化键盘上的数字
 initALLDig(view);
 }

 @Override
 public PopupWindow createPupupWindow() {
 return popupMenu;
 }

 @Override
 public void showPopupMenu() {
 if (popupMenu != null && popupMenu.isShowing()) {
  popupMenu.dismiss();
  StationEventCaptain.getInstance().fireEventDataChange(
   new StationEventData(IStationEventName.HEDGE_KEYBORAD_DIMISS, true));
 } else {
  //popupMenu.showAsDropDown(sourceEditView);

  int pianyi = dpToPx(activity, 8);
  if (keyboardType == IPopupMenu.MENU_TYPE_HEDGE_Left) {
  popupMenu.showAtLocation(keylinearlayout, Gravity.BOTTOM | Gravity.LEFT, pianyi, 0); //设置layout在PopupWindow中显示的位置 
  } else if (keyboardType == IPopupMenu.MENU_TYPE_HEDGE_RIGHT) {
  popupMenu.showAtLocation(keylinearlayout, Gravity.BOTTOM | Gravity.RIGHT, pianyi, 0); //设置layout在PopupWindow中显示的位置 
  } else {
  popupMenu.showAtLocation(keylinearlayout, Gravity.BOTTOM | Gravity.LEFT, 0, 0); //设置layout在PopupWindow中显示的位置 
  }

  // popupMenu.showAsDropDown(sourceView,
  // Math.round(sourceView.getX()),
  // (int) (sourceView.getY()));
 }
 }

 /**
 * 将dp转成像素的
 * 
 * @param context
 * @param dp
 * @return
 */
 static int dpToPx(final Context context, final float dp) {
 return (int) (dp * context.getResources().getDisplayMetrics().density);
 }

 @Override
 public void destroyPopupMenu() {
 popupMenu.dismiss();
 StationEventCaptain.getInstance()
  .fireEventDataChange(new StationEventData(IStationEventName.HEDGE_KEYBORAD_DIMISS, true));

 }

成功解决,有问题可以讨论。

您可能感兴趣的文章:

相关文章