popup window outside click set to false [setOutsid

2020-03-28 03:05发布

//create layoutinflator try {

        LayoutInflater inflator = LayoutInflater.from(this);

//create view

        final View menuview = inflater.inflate(R.layout.menu,
                (ViewGroup) findViewById(R.layout.dictionarylist));

        Button Menu = (Button) findViewById(R.id.Menu);

        Menu.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {

                final PopupWindow pw = new PopupWindow(menuview);//initialize popupwindow

                pw.showAtLocation(v, Gravity.CENTER, 0, 0);
                pw.update(0, 0, 200, 250);
                pw.setOutsideTouchable(false);//set outside touch to false

//onclick listener for the button inside popupwindow

                Button b1 = (Button) menuview.findViewById(R.id.btnClose);
                b1.setOnClickListener(new OnClickListener() {

                    // @Override
                    public void onClick(View v) {
                        pw.dismiss();

                    }

                });
                Button b2 = (Button) menuview.findViewById(R.id.btnQuiz);
                b2.setOnClickListener(new OnClickListener() {

                    // @Override
                    public void onClick(View v) {

                    }

                });
                Button b3 = (Button) menuview.findViewById(R.id.btnTopic);
                b3.setOnClickListener(new OnClickListener() {

                    // @Override
                    public void onClick(View v) {

                        InitialTask2 Task1 = new InitialTask2();
                        Task1.execute();

                    }

                });
                Button b4 = (Button) menuview
                        .findViewById(R.id.btnDictionarylist);
                b4.setOnClickListener(new OnClickListener() {

                    // @Override
                    public void onClick(View v) {

                        try{
                        if(getApplication() != null){
                            pw.dismiss();
                        }
                        else{
                        Intent i = new Intent();
                        i.setClass(getBaseContext(), Dictionarylist.class);
                        startActivity(i);
                        }
                        }
                        catch(Exception x){
                            x.getMessage();
                        }

                    }

                });


            }

        });
    } catch (Exception e) {
        e.getMessage();
    }
    }

popup window outside click does not work pw.setOutsideTouchable(false);. When clicking outside the popup window it perform action placed behind the popup window i.e parent window. enter image description here

3条回答
Melony?
2楼-- · 2020-03-28 03:27

Try the below code. Its working for me

popUp.setOutsideTouchable(false);
popUp.setFocusable(true);
popUp.showAtLocation(this.layout, Gravity.CENTER, 0, 0);
查看更多
祖国的老花朵
3楼-- · 2020-03-28 03:34

Try this:

pw.setTouchable(true);    
pw.setFocusable(false);    
pw.setOutsideTouchable(false);  

When window touchable is true, focusable is false, setOutsideTouchable() works.

If setOutsideTouchable(true), touch outside of popupwindow will dismiss, otherwise the outside of popupwindows still can be touchable without dismiss.

查看更多
劳资没心,怎么记你
4楼-- · 2020-03-28 03:36

To dismiss the popup window just add the following line of code

  popupWindow.setBackgroundDrawable(new ColorDrawable());

It worked for me and i am sure this is what you want.

查看更多
登录 后发表回答