欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-10-24 17:19:30
网上大多数都是说如何监听点击外部消失,但我遇到的问题是点击外面不让其消失,如下是我的解决方案,关键是:popupmenu.setbackgrounddrawable(nul...

网上大多数都是说如何监听点击外部消失,但我遇到的问题是点击外面不让其消失,如下是我的解决方案,关键是: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));

 }

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

以上这篇android popuwindow点击外部窗口不消失的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。