PopupWindow解雇不能正常工作

时间:2014-03-11 23:21:18

标签: android popup popupwindow dismiss

请帮帮我。

当我按下第一个按钮时,我在主活动中设置了一个弹出窗口。它工作正常,我可以通过我设置的关闭按钮关闭它,如果我想触摸外侧也是如此。

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

这是问题所在。当我现在关闭它时,我必须在弹出窗口外按两次或在弹出窗口外按一次然后按关闭按钮..然后我将返回到我的菜单屏幕。因此,按下两个按钮中的哪一个无关紧要,两个按钮都打开两个弹出窗口。这真的很烦人,我希望比我聪明的人可以帮助我。

这是我的menu.java

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

1 个答案:

答案 0 :(得分:0)

在initiatePopupWindow()中,您将创建两个弹出对话框。这就是为什么它的表现如此。

执行类似传递int值以区分调用initiatePopupWindow()方法的按钮(如

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

然后

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

使用单独的取消侦听器,这两个按钮是自定义侦听器适用于这两个对话框。