popupwindow closing issue by same button

烈酒焚心 提交于 2019-12-11 14:43:11

问题


I wanna close the PopupWindow by the same Button, But when I click on the Button again, it reopen the PopupWindow instead of closing it, and also should close the PopupWindow when i click outside of the PopupWindow any where, can anyone help me?

here is my code,

ivmainmenu.setOnClickListener(new OnClickListener() {

         @SuppressWarnings("null")
            @Override
            public void onClick(View v) {

             if(isShowing)
                {
                     PopupWindow popupWindow = null;
                    popupWindow.dismiss();
                     isShowing=false;
                }
                else
                {
                 isShowing=true;
                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.setBackgroundDrawable(new BitmapDrawable());
                        popupWindow.setOutsideTouchable(true);
                        popupWindow.setFocusable(true);

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

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

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

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

                }
         }
            });

When I tried this code I am getting an error when I click again on the same button, this is my logcat error which i get,

can anyone help to solve this error? thank you.


回答1:


As per my Way:

    int x=1;
    ivmainmenu.setOnClickListener(new OnClickListener() {

         @SuppressWarnings("null")
            @Override
            public void onClick(View v) {
           if((x%2)!=0){ 
            //Dismiss PopUp
           }else{
            //Show PopUp
           }
           x++;   
       }
    });

And also defined your popupWindow globally and make

popupWindow.setOutsideTouchable(false);
popupWindow.setCanceledOnTouchOutside(false)

Update:

PopupWindow popupWindow = null;
Boolean isShowing=false;
ivmainmenu.setOnClickListener(new OnClickListener() {

     @SuppressWarnings("null")
        @Override
        public void onClick(View v) {

         if(isShowing)
            {
              if(popupWindow != null && popupWindow.isShowing()){
                 popupWindow.dismiss();
                 }
                isShowing=false;
            }
            else
            {
             isShowing=true;
            LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
                View popupView = layoutInflater.inflate(R.layout.popupwindow, null);  
              popupWindow = new PopupWindow(popupView,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);  
                    popupWindow.showAsDropDown(ivmainmenu, 0,14);
                    popupView.setPadding(0, 0, 0, 10);    
                    popupWindow.setBackgroundDrawable(new BitmapDrawable());
                    popupWindow.setOutsideTouchable(false);
                    popupWindow.setCanceledOnTouchOutside(false)
                    popupWindow.setFocusable(true);

                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);
                        popupWindow.dismiss();
                        startActivity(intent);
                        isShowing=false;
                    }
                });

                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);
                         popupWindow.dismiss();
                         startActivity(intent);
                         isShowing=false;
                    }
                });

                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);
                         popupWindow.dismiss();
                         startActivity(intent);
                         isShowing=false;
                    }
                });

                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);
                         popupWindow.dismiss();
                         startActivity(intent);
                         isShowing=false;
                    }
                });

            }
     }
        });

give me feedback in this




回答2:


Change onClick() method as below:

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


                        if(popupWindow != null && popupWindow.isShowing()){
                           popupWindow.dismiss();
                        }

                    } 



回答3:


If you want to close the popup with same button and on clicking anywhere outside then you will have to implement

  • The touch interceptor and dismiss the popup when action is MotionEvent.ACTION_OUTSIDE.
  • Set the popup to be focusable using setFocusable(true)

Setting focusable ensures that popup can grab outside touch events and since it will also capture the click on the menuitem or a button, It ensures that popup is not launched again if it is already showing.

To, put the above logic in code, you have to do the following.

 final MenuItem popupMenu= menu.findItem(R.id.action_open_popup);
    popupMenu.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (window == null) {
                View contentView = getLayoutInflater(null).inflate(R.layout.popup_menu, null);
                window = new PopupWindow(contentView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
                window.setBackgroundDrawable(new BitmapDrawable(getResources(), ""));
                window.setOutsideTouchable(true);
                window.setFocusable(true);
                window.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                            window.dismiss();
                            return true;
                        }
                        return false;
                    }
                });
            }
            //anchor as the menuitem this is in fragment so.
            window.showAsDropDown(getActivity().findViewById(R.id.action_open_popup));
            return true;
        }
    });


来源:https://stackoverflow.com/questions/22267224/popupwindow-closing-issue-by-same-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!