通过返回键关闭Android弹出窗口

8

我创建了一个安卓应用程序,其中包含一个弹出屏幕。但是当我按返回按钮时,弹出屏幕没有关闭。

我尝试使用onBackPressed()。但它不起作用。

有人能告诉我该怎么做吗?

谢谢,

Shankar


2
如果您提供您编写的代码片段将会更有帮助。我们不知道您是在引用Dialog还是PopupWindow的实例。 - IgorGanapolsky
8个回答

38

你需要做的是在初始化PopupWindow后调用setBackgroundDrawable。例如:

myPopup.setBackgroundDrawable(new BitmapDrawable());

2
  1. 确保在显示弹出窗口之前调用此函数。
  2. 由于没有参数的构造函数已经被弃用,可能需要将一个资源对象传递给该构造函数。
  3. 更多信息请参考:https://dev59.com/o3A75IYBdhLWcg3wv7_A
- SilithCrowe
2
这个很好用。但是请确保将focusable设置为true。如果它是false,那么返回按钮就不会自动工作,你需要在调用的活动中实现hotveryspicy的解决方案。 - jim
不确定为什么这个有效,但它可以与正常的代码一起关闭弹出窗口。 - kabuto178
4
请注意,它不一定是BitmapDrawable,我使用的是ColorDrawable,它可以愉快地构建没有参数的对象。 - Newtz

8

最近我正在使用ListPopupWindow(android.support.v7.internal.widget.ListPopupWindow),当我调用时,返回按钮开始工作。

popupWindow.setModal(true);

无论我在`setBackgroundDrawable`方法中设置什么,都无法解决其他解决方案中提到的问题。

4
LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup_window_country_list, null);
    countryPopup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    countryPopup.setBackgroundDrawable (new BitmapDrawable());

    countryPopup.setFocusable(true); //Make Here True For back press dismiss

    countryPopup.setOutsideTouchable(true); 

    countryPopup.setTouchInterceptor(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {

                countryPopup.dismiss();


                return true;

            }

            return false;

        }
    });

3

100%的弹出窗口将在后退键按下时关闭。使用以下代码替换您的弹出窗口代码:

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

}

(或者)

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

2
以下代码完美运行。 因此,请在您的活动中覆盖以下函数。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     //Changes 'back' button action
    if(keyCode==KeyEvent.KEYCODE_BACK)
    {
        if(!popUpHelper.isPopupShowing()){
            onBackPressed();
        }else{
            popUpHelper.dismiss();
        }
    }

    return super.onKeyDown(keyCode, event);
}


class PopupHelper {
 PopupWindow popupWindowAttachment;
  public void initAndShow(Activity context,int mToolbarHeight){

   View layout = activity.getLayoutInflater().inflate(
            R.layout.activity_chat_attachment_popup, null);
    //create and show and Make sure popup window focusable should be false
    popupWindowAttachment = new PopupWindow(layout,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindowAttachment.showAsDropDown(layout, 0, mToolbarHeight
            + (mToolbarHeight / 2) - 5);

  }

  public void dismiss() {
    if (isPopupShowing())
            popupWindowAttachment.dismiss();
  }
  public boolean isPopupShowing() {
    return popupWindowAttachment==null?false:popupWindowAttachment  
                 .isShowing();
  }
}

2
//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"`

希望这对您有用(我是新用户)


0
通常情况下,弹出窗口在按下返回按钮后会关闭。如果需要在弹出窗口关闭时处理特定事项,您可以使用以下方法:
popupWindow.setOnDismissListener

注意:- 我使用 Kotlin 进行开发,它对我很有效。


-1
popup.setBackgroundDrawable(new BitmapDrawable());

popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //do your code here
            }
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接