android中的多个弹出窗口

时间:2015-02-18 10:35:39

标签: android popup

我在单个活动中有两个线性布局。 我想在点击时在每个上面显示自定义弹出窗口。 当我单击第一个布局,弹出窗口,然后单击第二个布局时,将显示两个弹出窗口。我怎样才能一次只显示一个弹出窗口?

这是我的代码

    workLinearLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    LayoutInflater layoutInflater=          
   (LayoutInflater)getBaseContext()
    .getSystemService(LAYOUT_INFLATER_SERVICE);
     View popupView =   layoutInflater.inflate
    (R.layout.activity_your_places__work__popup, null);
     updateTextView = (TextView)    
     popupView.findViewById(R.id.UpdateTextView);
     updateTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Your_Places2Activity.this,       
                    UpdateWorkAddressActivity.class);
                    startActivity(i);
                }
            });
            deleteTextView = (TextView)     
            popupView.findViewById(R.id.DeleteTextView);
            deleteTextView.setOnClickListener(new View.OnClickListener()                
            {
                @Override
                public void onClick(View v) {
                   //code to delete address
                }
            });
            popupWindowWork = new PopupWindow(
                    popupView,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            //dismiss other popup if is showing
            if(popupWindowHome.isShowing())
            {
                popupWindowHome.dismiss();}
            //display popup
            popupWindowWork.showAsDropDown(workLinearLayout, 0, -70);

           }
         });

我在其他线性布局上做了同样的事情

1 个答案:

答案 0 :(得分:1)

popupWindowWork = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
if(popupWindowHome.isShowing())
{
    popupWindowHome.dismiss();
}

在此行中,如果显示popupWindowHome,则您将关闭popupWindowHome对话框,其中popupWindowHome为新对话框。在构造函数调用之前移动if语句。

if(popupWindowHome != null && popupWindowHome.isShowing())
{
    popupWindowHome.dismiss();
}
popupWindowWork = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);