Issue dismissing popup window

后端 未结 2 1782
暖寄归人
暖寄归人 2020-12-10 19:40

I have a popup menu implemented , which shows up on click of a button. This is my onclick method.

public void showOverflow(View view) {

    boolean click =          


        
相关标签:
2条回答
  • 2020-12-10 20:04

    You should change the setOutsideTouchable call's parameter to true: pw.setOutsideTouchable(false);

    Controls whether the pop-up will be informed of touch events outside of its window. This only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind. The default is false.

    If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to one of the update() methods.

    Parameters: touchable true if the popup should receive outside touch events, false otherwise

    On the other hand, what is the click local variable supposed to do? It is set to true, so it will always force the pw to pop up, whenever the showOverflow method is called, and for no reason it is set to false later, because it's life cycle ends as you leave that method.

    Your code should look something like this:

    private LayoutInflater inflater;
    private Button action;
    private PopupWindow pw;
    private View popupView;
    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        popupView = inflater.inflate(R.layout.overflow_layout, null, false);
    
        action = (Button) findViewById(R.id.action);
        action.setOnClickListener(this);
    }
    
    public void showOverflow()
    {
        pw = new PopupWindow(getApplicationContext());
        pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pw.setOutsideTouchable(true);
    
        pw.setContentView(popupView);
        pw.showAsDropDown(action, 0, 0);
    }
    

    The getApplicationContext() shoud be used in case you are inside an Activity class. Otherwise you should get the Context as a parameter.

    0 讨论(0)
  • 2020-12-10 20:05

    change pw.setOutsideTouchable(true); to pw.setOutsideTouchable(false);

    0 讨论(0)
提交回复
热议问题