单击外部后弹出窗口不会被解除

时间:2016-02-10 05:15:36

标签: android popupwindow

我使用以下代码在工具栏下面创建弹出窗口:

   private void initializePopUpWindow() {
        //inflate the popupwindow_attachment.xml
        LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
        LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
        popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);

        //Displaying the popup at a specific location
       // popupWindow.showAtLocation(layout, Gravity.TOP, 0, 150);
        popupWindow.showAsDropDown(toolbar,0,0);

        //Close the popup when touch outside
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);              
    }

我已经在工具栏下面成功添加了一个弹出窗口,但是当我点击它外面时它没有删除。没有功能正常工作。请帮我解决问题。

编辑工作代码:

我做了一些愚蠢的错误。我现在修好了。

1.onCreate()方法

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_chat);

        //Toolbar
        toolbar = (Toolbar) findViewById(R.id.toolbarSingleChat);
        toolbar.setNavigationIcon(R.drawable.back); // Setting Navigation Icon in the Toolbar
        setSupportActionBar(toolbar);

 LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
        LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
        popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

        //Close the popup when touch outside
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}

2.onoptionsItemSelected()

 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_viewContacts:
            return true;
        case R.id.action_media:
            return true;
        case R.id.action_search:
            return true;
        case R.id.action_block:
            return true;
        case R.id.action_email_chat:
            return true;
        case R.id.action_clear_chat:
            return true;
        case R.id.action_attach:
            initializePopUpWindow();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

3.initializePopUpWindow()方法:

  private void initializePopUpWindow() {
  popupWindow.showAsDropDown(toolbar, 0, 0);
}

这里我已经在活动的onCraeteMethod()中初始化了popupWindow。同样,在onCreate()方法中添加了处理弹出窗口关闭的代码。我已经在onOptionsItemSelected()方法中调用了创建弹出窗口的代码现在它为我工作。谢谢你的帮助。

4 个答案:

答案 0 :(得分:1)

如果您触摸窗外,请尝试设置setBackgroundDrawable PopupWindow关闭窗口的popUp.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(mContext,android.R.color.transparent))); 。 使用以下代码。在代码中添加以下行。

$string ='<a href="http://facebook.com/feelingblue">Facebook</a>';
if (strpos($string, 'facebook') !== false) {
    $facebookUrl = "http://www.facebook.com";
    $regEx = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
    $newUrl = preg_replace($regEx,$facebookUrl,$string);
}
echo $newUrl;

有关详细信息,请查看以下链接。Ans.

答案 1 :(得分:1)

请尝试在PopupWindow上设置 setBackgroundDrawable ,如果你触摸它,应关闭窗口。

=AGGREGATE(14, 6, ROW(1:17)/(SUBTOTAL(9, OFFSET(B1, 0, 0, ROW(1:17), 1))<D7), 1)

答案 2 :(得分:0)

方法setOutsideTouchable不会让您的弹出窗口在外面触摸时解除。它只允许弹出窗口接收在其外部发生的触摸事件。我认为你有意愿用setTouchInterceptor注册一个触摸监听器,然后在窗口上调用dismiss()。

答案 3 :(得分:0)

添加这两行。它会完成你的工作。

myPopupWindow.setBackgroundDrawable(new BitmapDrawable());
myPopupWindow.setOutsideTouchable(true);