Android PopupWindow 点击外面取消 Android PopupWindow 点击外面取消实现代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android PopupWindow 点击外面取消 Android PopupWindow 点击外面取消实现代码

  2021-03-21 我要评论
想了解Android PopupWindow 点击外面取消实现代码的相关内容吗,在本文为您仔细讲解Android PopupWindow 点击外面取消的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,PopupWindow,点击外面取消,下面大家一起来学习吧。
private void showPopupView()
  {
    if (mPopupWindow == null)
    {
      View view = getLayoutInflater().inflate(R.layout.newest_layout, null);
      mPopupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      mPopupWindow.setFocusable(true);//需要设置为ture,表示可以聚焦

        //需要设置背景,用物理键返回的时候
			        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources()));
			        mPopupWindow.setOutsideTouchable(true);

      view.setOnTouchListener(new OnTouchListener()// 需要设置,点击之后取消popupview,即使点击外面,也可以捕获事件
      {
        public boolean onTouch(View v, MotionEvent event)
        {
          if (mPopupWindow.isShowing())
          {
            Trace.Log("-------------------onTouch------------");
            mPopupWindow.dismiss();
          }
          return false;
        }
      });

    }

    if (mPopupWindow.isShowing())
    {
      mPopupWindow.dismiss();
    }
    else
    {
      View parent = findViewById(R.id.newest);
      mPopupWindow.showAsDropDown(parent);// 显示再指定控件的下面
    }

  }

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们