其他分享
首页 > 其他分享> > android – 通过后退按钮关闭弹出窗口

android – 通过后退按钮关闭弹出窗口

作者:互联网

我想通过单击弹出窗口外部或后退按钮来关闭弹出窗口,但是当单击后退按钮我的应用程序退出时,而不是退出应用程序我想要关闭弹出窗口.

这是我的代码,

ivmainmenu.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            LayoutInflater layoutInflater 
             = (LayoutInflater)getBaseContext()
              .getSystemService(LAYOUT_INFLATER_SERVICE);  
            View popupView = layoutInflater.inflate(R.layout.popupwindow, null);  
          final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.FILL_PARENT,
                  LayoutParams.WRAP_CONTENT);  
                popupWindow.showAsDropDown(ivmainmenu, 0,14);
                popupView.setPadding(0, 0, 0, 10);
                popupWindow.showAsDropDown(ivmainmenu);

                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                popupWindow.setOutsideTouchable(false);

                TextView tvpopupwork = (TextView)popupView.findViewById(R.id.tvpopupwork);
                TextView tvpopupabout = (TextView)popupView.findViewById(R.id.tvpopupabout);
                TextView tvpopupservices = (TextView)popupView.findViewById(R.id.tvpopupservices);
                TextView tvpopupcontact = (TextView)popupView.findViewById(R.id.tvpopupcontact);

                Typeface typeFace2 =  Typeface.createFromAsset(getAssets(),"fonts/arboriaboldregular.ttf");
                tvpopupwork.setTypeface(typeFace2);
                tvpopupabout.setTypeface(typeFace2);
                tvpopupservices.setTypeface(typeFace2);
                tvpopupcontact.setTypeface(typeFace2);

                tvpopupwork.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(Home.this,Ourwork.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                });

                tvpopupabout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(Home.this,Aboutus.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);  
                    }
                });

                tvpopupservices.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent(Home.this,Services.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                });

                tvpopupcontact.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent(Home.this,Contact.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                });
                 ivmainmenu.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    popupWindow.dismiss();
                }
           }
        });

它给了我想要的结果,但当我关闭菜单时,它不再打开,我想再次打开它,那我该怎么办?
谢谢.

解决方法:

更换

popupWindow.setOutsideTouchable(false);

有了这个

popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);

标签:android,android-popupwindow
来源: https://codeday.me/bug/20191001/1838454.html