2013-03-06 100 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)