2013-03-06 56 views
0

我在android中实现了一个弹出窗口。当我在弹出窗口外单击时,我可以忽略它,但是当我单击相同的图像视图/按钮时,我想解雇它。popup关闭android

1.on click on button1 it opens pop up 
2.If I click outside it closes pop up 
3.If I click on button1(when pop up is open), it closes pop and reopens it again 

我要的是 如果我在Button1的Click(当弹出打开时),它只是关闭弹出和DONOT重新打开,除非第二次点击。

这可能吗?

注:我不想在弹出窗口内有一个关闭按钮。

pop_one.setOnClickListener(new OnClickListener() { 

       public void onClick(final View v) { 

        pop_one.getLocationOnScreen(location); 

         p = new Point(); 

         p.x = location[0]; 

         p.y = location[1]; 

        popupshow(one_one_text,pop_one); 


       } 
      }); 

而且其方法是:

public void popupshow(String pop_text, ImageView pop_one) { 


    int width = display.getWidth(); // deprecated 
    System.out.println("jsfjsfjnsdf"+width); 
    int new_width = width-(width/6) ; 

    LayoutInflater layoutInflater = (LayoutInflater) getActivity() 
      .getBaseContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(
      R.layout.career_options_popup_short, null); 

    final PopupWindow popupWindow = new PopupWindow(popupView, new_width, 
      LayoutParams.WRAP_CONTENT); 

    TextView popup_text = (TextView) popupView 
      .findViewById(R.id.popup_text); 
    popup_text.setMinimumWidth(300); 
    popup_text.setText(pop_text); 
    popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
    popupWindow.setOutsideTouchable(true); 

    popupWindow.showAsDropDown(popupView,p.x/10, p.y +p.x/8); 

    System.out.println("value of x" + p.x+" "+p.y); 

    //System.out.println("value of new width " + new_width+" get width "+popupWindow.getWidth()+" text box width "+popup_text.getWidth()+"pop view"+popupView.getWidth()); 
    popupWindow.setFocusable(true); 



} 

问候, ASMI

回答

-1

你需要的是一个boolean isShowing设置此boolean在功能popupshow

这样

public void popupshow(String pop_text, ImageView pop_one) { 

    isShowing=true; 
    int width = display.getWidth(); // deprecated 
    System.out.println("jsfjsfjnsdf"+width); 
    int new_width = width-(width/6) ; 

    LayoutInflater layoutInflater = (LayoutInflater) getActivity() 
      .getBaseContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(
      R.layout.career_options_popup_short, null); 

    final PopupWindow popupWindow = new PopupWindow(popupView, new_width, 
      LayoutParams.WRAP_CONTENT); 

    TextView popup_text = (TextView) popupView 
      .findViewById(R.id.popup_text); 
    popup_text.setMinimumWidth(300); 
    popup_text.setText(pop_text); 
    popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
    popupWindow.setOutsideTouchable(true); 

    popupWindow.showAsDropDown(popupView,p.x/10, p.y +p.x/8); 

    System.out.println("value of x" + p.x+" "+p.y); 

    //System.out.println("value of new width " + new_width+" get width "+popupWindow.getWidth()+" text box width "+popup_text.getWidth()+"pop view"+popupView.getWidth()); 
    popupWindow.setFocusable(true); 



} 

是真实的,现在当你在button1单击只是检查这个boolean

pop_one.setOnClickListener(new OnClickListener() { 

       public void onClick(final View v) { 

        if(isShowing) 
        { 
         popupWindow.dismiss(); 
         isShowing=false; 
        } 
        else 
        { 
        pop_one.getLocationOnScreen(location); 

        p = new Point(); 

        p.x = location[0]; 

        p.y = location[1]; 

        popupshow(one_one_text,pop_one); 

} 
       } 
      }); 
2

我认为这将解决您的问题,电话本上的按钮,点击

popupWindow.dismiss(); 
+0

我应该在哪里放置这个popupWindow.dismiss();我不想在弹出窗口中有一个关闭按钮 – Asmi 2013-03-06 07:20:46

0

您需要设置弹出窗口的属性setOutsideTouchable(false)