0

我正在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(当弹出窗口打开时),它只会关闭弹出窗口并且不重新打开,除非第二次点击。

这可能吗?

注意:我不想在弹出窗口中有关闭按钮。

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



}

问候,阿斯米

4

3 回答 3

2

我认为这将解决您的问题在按钮单击时调用

popupWindow.dismiss();
于 2013-03-06T07:19:47.113 回答
0

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

于 2013-03-06T07:36:12.700 回答
-1

您需要的是booleanisShowingboolean在函数中将其设置为 truepopupshow

像这样

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

}
                 }
             });
于 2013-03-06T07:36:57.907 回答