android - 单击设置为 false [setOutsideTouchable(false)] 外的弹出窗口不起作用

标签 android popupwindow layout-inflater

//创建layoutinflator 尝试{

        LayoutInflater inflator = LayoutInflater.from(this);

//创建 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监听器

                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);。当在弹出窗口外单击时,它会执行放置在弹出窗口后面的操作,即父窗口。 enter image description here

最佳答案

试试这个:

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

当 window touchable 为 true,focusable 为 false 时,setOutsideTouchable() 有效。

如果setOutsideTouchable(true),弹出窗口外的触摸将关闭,否则弹出窗口的外部仍然可以触摸而不关闭。

关于android - 单击设置为 false [setOutsideTouchable(false)] 外的弹出窗口不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13217946/

相关文章:

android - 重新安装后应用程序可以播放

java - Android Studio 不断导入 import sun.jvm.hotspot.utilities.IntArray

.net - .NET MVC 中的弹出窗口

android - 使用 LayoutInflator 的膨胀方法时的不同结果

android - 修改了我的项目包,但更改不影响应用程序

android - 每分钟更新 TextView 的最佳方式

Angular 2弹出窗口

android - 使用 MVVM 模型在 Xamarin Android 中弹出 View

java - 单击按钮后关闭警报对话框

android - 滚动回收器 View 时如何防止项目重复