2

//创建layoutinflator try {

        LayoutInflater inflator = LayoutInflater.from(this);

//创建视图

        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监听器

                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();
    }
    }

点击外的弹出窗口不起作用pw.setOutsideTouchable(false);。在弹出窗口外部单击时,它会执行放置在弹出窗口(即父窗口)后面的操作。 在此处输入图像描述

4

3 回答 3

8

尝试这个:

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

当 window touchable 是true,focusable 是falsesetOutsideTouchable()工作。

如果setOutsideTouchable(true), touch outside of popupwindow 将关闭,否则 popupwindows 的外部仍然可以触摸而不关闭。

于 2013-01-30T10:46:40.313 回答
7

要关闭弹出窗口,只需添加以下代码行

  popupWindow.setBackgroundDrawable(new ColorDrawable());

它对我有用,我相信这就是你想要的。

于 2013-10-08T05:20:25.767 回答
0

试试下面的代码。它为我工作

popUp.setOutsideTouchable(false);
popUp.setFocusable(true);
popUp.showAtLocation(this.layout, Gravity.CENTER, 0, 0);
于 2014-06-16T08:03:39.263 回答