Close Android popup window with back press

后端 未结 8 2023
春和景丽
春和景丽 2021-01-05 02:42

I have created a android application where I have created a popup screen. But when I am pressing the back button, popup is not getting closed.

I have tried with onB

相关标签:
8条回答
  • 2021-01-05 03:10
    //here "popUp" is ref of PopupWindow
    
    popUp.setBackgroundDrawable(new BitmapDrawable());// it is most important peace of code
    
    // For Key Listeners
    
    View v = popUp.getContentView();
    
    //Here assigning the key Listners
    
        v.setOnKeyListener(this);
    
        @Override   
        public boolean onKey(View v, int keyCode, KeyEvent event) {
    
            if(keyCode == KeyEvent.KEYCODE_BACK) popUp.dismiss();
    
            return false;
    
        }//Implements the KeyListener
         //and be careful we should implement "OnKeyListener"`
    

    I hope it is useful (I'm the new user)

    0 讨论(0)
  • 2021-01-05 03:13

    100% popup will dismiss on back press. Replace your Popup code with this below code

    public void popup() {
    
        View popUpView_pur = getActivity().getLayoutInflater().inflate(R.layout.popup, null);
        PopupWindow popuplayout_pur = new PopupWindow(popUpView_pur, -1, -1, true);
        popuplayout_pur.setBackgroundDrawable(new BitmapDrawable());
        popuplayout_pur.setOutsideTouchable(true);
        popuplayout_pur.showAtLocation(popUpView_pur, 17, 0, 0);
    
    }
    

    (or)

    public void popup() {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popuplayout, null, false);
    
        PopupWindow pw = new PopupWindow(getActivity());
        pw.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        pw.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
    
        pw.setTouchable(true);
        pw.setFocusable(true);
        pw.setOutsideTouchable(true);
        pw.setContentView(popupView);
    
        pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
    }
    
    0 讨论(0)
提交回复
热议问题