2011-08-14 23 views
0

我有問題使彈出不可見。當我按下顯示它變得可見時,但當我按下取消按鈕(取消按鈕在彈出)它不會變得不可見,但它通過代碼傳遞正常,沒有任何東西。Popup不會變爲不可見,但它會通過代碼並且什麼也沒有發生

final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup, 
      null, false), 300, 300, true); 

    View layout = inflater.inflate(R.layout.popup, 
      (ViewGroup) findViewById(R.id.llPopup)); 

    Button btnOK = (Button) layout.findViewById(R.id.btnOK); 
    Button btnCancel = (Button) layout.findViewById(R.id.btnCancel); 
    btnCancel.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if (pw != null) { 
       pw.dismiss(); 
      } 

     } 
    }); 

這是popup.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:padding="10dip" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#AAAAAAAA" 
    android:id="@+id/llPopup" 
    > 


<LinearLayout 
    android:orientation="horizontal" 
    android:padding="10dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
> 
<TextView 
    android:id="@+id/txtSearchBy" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Search By:" 
/> 
    <RadioGroup 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:orientation="vertical"> 
     <RadioButton 
     android:id="@+id/rbPrice" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Time" /> 
     <RadioButton 
     android:id="@+id/rbDuration" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Price" /> 
     <RadioButton 
     android:id="@+id/rbLongitude" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Longitude" /> 
    </RadioGroup> 

</LinearLayout> 



    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 

     > 
     <Button 
      android:id="@+id/btnCancel" 
      android:layout_width="100sp" 
      android:layout_height="wrap_content" 
      android:text="Cancel" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
     /> 

     <Button 
      android:id="@+id/btnOK" 
      android:layout_width="100sp" 
      android:layout_height="wrap_content" 
      android:text="OK" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
     /> 

    </RelativeLayout> 


</LinearLayout> 

我做出錯誤的任何人可以幫助我嗎?

回答

2
View layout = inflater.inflate(R.layout.popup, 
     (ViewGroup) findViewById(R.id.llPopup)); 

final PopupWindow pw = new PopupWindow(layout, 300, 300, true); 

Button btnOK = (Button) layout.findViewById(R.id.btnOK); 
Button btnCancel = (Button) layout.findViewById(R.id.btnCancel); 
btnCancel.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if (pw != null) { 
      pw.dismiss(); 
     } 

    } 
}); 

每次調用inflate方法是創建一個新的View時間;因此,您設置偵聽器的取消按鈕與彈出框內的取消按鈕不同。正如你上面看到的,同樣的View用於:初始化彈出窗口並設置點擊監聽器。

+0

@Christian謝謝,現在工作! – Damir

相關問題