首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PopupWindow dismiss不能正常工作

PopupWindow dismiss不能正常工作
EN

Stack Overflow用户
提问于 2014-03-12 07:21:18
回答 1查看 1.8K关注 0票数 0

请帮帮我。

我已经在我的主活动中设置了一个弹出窗口,当我按下第一个按钮时。它工作得很好,我可以通过我设置的关闭按钮关闭它,如果我想触摸外部的话也可以。

但我需要在同一个活动中有多个弹出窗口。我有各种各样的按钮,如果你按下它们,他们应该会通过弹出窗口给你更多信息。现在我已经创建了我的第二个按钮,当我打开弹出窗口时,它就可以工作了。

这是问题所在。当我现在关闭它时,我必须在弹出窗口外按两次,或在弹出窗口外按一次,然后按关闭按钮。只有这样,我才会返回到我的菜单屏幕。所以我按下这两个按钮中的哪一个并不重要,这两个按钮都会打开两个弹出窗口。这真的很烦人,我希望有比我更聪明的人能帮助我。

这是我的menu.java

代码语言:javascript
复制
package com.fadi.enumbers;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

public class menu extends Activity {
    Button btnClosePopup;
    Button btnCreatePopup;
    Button btnCreatePopup2;
    Bitmap bitmap;
    Resources res;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnCreatePopup = (Button) findViewById(R.id.btn_e100);
        btnCreatePopup2 = (Button) findViewById(R.id.btn_e100ii);
        btnCreatePopup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatePopupWindow();
            } 
        });

        btnCreatePopup2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatePopupWindow();
            } 
        });

    }

    private PopupWindow pwindo;
    private PopupWindow pwindo2;

    private void initiatePopupWindow() {
        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) menu.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.screen_popup,(ViewGroup) findViewById(R.id.popup_element));

            pwindo = new PopupWindow(layout, 370, 450, true);
            pwindo.setBackgroundDrawable(new BitmapDrawable(res, bitmap));
            pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

            btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) menu.this        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout2 = inflater.inflate(R.layout.screen_popup2, (ViewGroup)findViewById(R.id.popup_element));
            pwindo2 = new PopupWindow(layout2, 370, 450, true);
            pwindo2.setBackgroundDrawable(new BitmapDrawable(res, bitmap));
            pwindo2.showAtLocation(layout2, Gravity.CENTER, 0, 0);

            btnClosePopup = (Button) layout2.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OnClickListener cancel_button_click_listener = new OnClickListener() {
        public void onClick(View v) {
            pwindo.dismiss();
        }
    };    
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-12 14:36:57

在您的initiatePopupWindow()中,您正在创建两个弹出对话框。这就是为什么它会那样做的原因。

做一些事情,比如传递一个int值,以区分调用initiatePopupWindow()方法的按钮,如下所示

代码语言:javascript
复制
btnCreatePopup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatePopupWindow(1);
            } 
        });

        btnCreatePopup2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatePopupWindow(2);
            } 
        });

然后

代码语言:javascript
复制
private void initiatePopupWindow(int popupNo) {
swich(popupNo)
{
case 1:
        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) menu.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.screen_popup,(ViewGroup) findViewById(R.id.popup_element));

            pwindo = new PopupWindow(layout, 370, 450, true);
            pwindo.setBackgroundDrawable(new BitmapDrawable(res, bitmap));
            pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

            btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }
break;
case 2:

        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) menu.this        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout2 = inflater.inflate(R.layout.screen_popup2, (ViewGroup)findViewById(R.id.popup_element));
            pwindo2 = new PopupWindow(layout2, 370, 450, true);
            pwindo2.setBackgroundDrawable(new BitmapDrawable(res, bitmap));
            pwindo2.showAtLocation(layout2, Gravity.CENTER, 0, 0);

            btnClosePopup = (Button) layout2.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }
break;
    }

为这两个按钮使用单独取消侦听器是对侦听器进行自定义,以便为这两个对话框工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22338800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档