2011-09-01 4 views
6

Cách mở PopupWindow trên Android và cho phép tất cả các thành phần khác có thể chạm vào mà không bỏ qua PopupWindow?Mở PopupWindow và để cho các bên ngoài vẫn có thể chạm vào

Đây là cách nó được tạo ra:

public class DynamicPopup { 
    private final PopupWindow window; 
    private final RectF rect; 
    private final View parent; 
    private final RichPageView view; 

    public DynamicPopup(Context context, RichPage page, RectF rectF, View parent) { 
     this.parent = parent; 
     rect = rectF; 

     window = new PopupWindow(context); 

     window.setBackgroundDrawable(new BitmapDrawable()); 
     window.setWidth((int) rect.width()); 
     window.setHeight((int) rect.height()); 
     window.setTouchable(true); 
     window.setFocusable(true); 
     window.setOutsideTouchable(true); 

     view = new RichPageView(context, page, false); 
     window.setContentView(view); 

     view.setOnCloseListener(new Listener(){ 
      @Override 
      public void onAction() { 
       window.dismiss(); 
      } 
     }); 


    } 

    public void show() { 
     window.showAtLocation(parent, Gravity.NO_GRAVITY, (int) rect.left, (int) rect.top); 
    } 
} 
+2

Bạn đã thử phương pháp này chưa? http://developer.android.com/reference/android/widget/PopupWindow.html # setOutsideTouchable% 28boolean% 29 – ernazm

+0

@ernazm, bạn nên chuyển câu trả lời này. – Phil

+0

Tôi không chắc chắn phương pháp này hoạt động như dự định kể từ khi tôi không sử dụng nó (Android SDK có rất nhiều thứ tbh lỗi) và câu hỏi này đã được nâng lên. – ernazm

Trả lời

8

Theo javadocs

Controls dù các cửa sổ pop-up sẽ được thông báo về các sự kiện liên lạc bên ngoài cửa sổ của nó. Điều này chỉ có ý nghĩa cho các pop-up có thể sờ nhưng không thể đặt tiêu điểm

Vì vậy, dòng của bạn

window.setFocusable(true); 

gây phương pháp setOutsideTouchable() để làm gì cả.

+0

Ngay cả với window.setFocusable (false); điều này đóng theo mặc định –

1

Cho rằng bạn phải làm cho cửa sổ Tuỳ chỉnh Popup. Trong đó u phải gọi bố trí khác. Vì vậy, theo cách này, bạn cũng có thể truy cập vào thành phần khác. Nó chỉ là một loại Bố cục. Nhưng bạn có thể thiết lập sự xuất hiện của nó như là cửa sổ bật lên.

<!-- POPUP MENU --> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/popup_window" 
     android:orientation="vertical" 
     android:gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/fullwindowborderforpopup" 
     > 



     <LinearLayout 

      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 

      android:padding="1px" 
      android:layout_marginTop="15dip" 
      android:layout_marginLeft="20dip" 
      android:layout_marginRight="20dip" 
      android:layout_marginBottom="10dip" 
      android:background="@drawable/borderforpopup" 
      > 
      <LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_horizontal" 
       android:layout_marginTop="5dip" 
       android:layout_marginLeft="3dip" 
       android:layout_marginRight="3dip" 
       android:layout_marginBottom="3dip"> 

       <TextView 
        android:id="@+id/tital_of_popup" 
        android:gravity="center_vertical|center_horizontal" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="Event Registration" 
        android:textStyle="bold" 
        android:textColor="#ffffff" 
        /> 
      </LinearLayout> 

      <LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_horizontal" 
       android:layout_marginTop="5dip" 
       android:layout_marginLeft="3dip" 
       android:layout_marginRight="3dip" 
       android:layout_marginBottom="3dip" 
      > 

       <TextView 
        android:id="@+id/message_of_popup" 
        android:gravity="center_vertical|center_horizontal" 
        android:layout_height="wrap_content" 
        android:text="Please fill all the data" 
        android:layout_width="wrap_content" 
        android:textStyle="normal" 
        android:textColor="#ffffff" /> 
     </LinearLayout> 
     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:layout_marginTop="5dip" 
      android:layout_marginLeft="6dip" 
      android:layout_marginRight="6dip" 
      android:layout_marginBottom="9dip" 
       > 


      <Button 
       android:layout_height="fill_parent" 
       android:id="@+id/okbutton" 
       android:text="OK" 
       android:textColor="#ffffff" 
       android:shadowColor="#000000" 
       android:gravity="center" 
       android:layout_width="fill_parent" 
       android:background="@drawable/buttonborderframe"/> 
     </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

dùng thử. Và bạn có thể ẩn và hiển thị bố cục bất cứ khi nào bạn muốn. Và các trường khác có thể truy cập được.

+0

Tôi thực sự cần phải thêm PopupWindows vào thời gian chạy, tôi không có sau đó cho đến khi nó runnning. –

+1

Sau đó, bạn cần phải thực hiện phong cách tùy chỉnh cho cửa sổ bật lên và đặt thuộc tính như bạn muốn. –

12

giống như ernazm nói

Theo javadocs

Controls dù các cửa sổ pop-up sẽ được thông báo về các sự kiện liên lạc bên ngoài cửa sổ của nó. Điều này chỉ có ý nghĩa cho các pop-up có thể sờ nhưng không thể đặt tiêu điểm

và nó làm việc trên

window.setTouchable(true); 
window.setFocusable(false); 
window.setOutsideTouchable(false); 

Khi cửa sổ touchalbe là đúng, đặt tiêu điểm là sai, setOutsideTouchable() là công việc, nếu setOutsideTouchable (true), chạm vào bên ngoài của popupwindow sẽ bỏ qua, nếu không bên ngoài của popupwindows vẫn có thể được touchable mà không cần loại bỏ.

+1

'setTouchable()' là theo mặc định 'true'. và 'setOutsideTouchable()' là mặc định 'false'. Vì vậy, tất cả những gì bạn thực sự cần đặt là 'setFocusable (false)'. Nhưng có vẻ như người nghe sự kiện không làm việc trên các khung nhìn bên trong cửa sổ bật lên khi nó không tập trung được. –

+0

Đó là một chút khó khăn. Các tài liệu là đúng nhưng vì một lý do nào đó, tôi luôn muốn đặt tiêu điểm thành sự thật. Ba thiết lập trên làm việc. –

+0

Trình nghe sự kiện của tôi dành cho người quay phim hoạt động nhưng người nghe edittext của tôi thì không. –

0

Hãy thử thêm này để mã của bạn:

window.setOutsideTouchable(true); 

Từ Android documentation:

void setOutsideTouchable (boolean touchable) 

Controls dù các cửa sổ pop-up sẽ được thông báo về các sự kiện liên lạc bên ngoài cửa sổ của nó. Điều này chỉ có ý nghĩa đối với các cửa sổ bật lên có thể chạm được nhưng không thể lấy nét, có nghĩa là các chạm bên ngoài cửa sổ sẽ được chuyển đến cửa sổ phía sau. Giá trị mặc định là false.

Nếu cửa sổ bật lên đang hiển thị, việc gọi phương thức này sẽ chỉ có hiệu lực vào lần tiếp theo cửa sổ bật lên được hiển thị hoặc thông qua cuộc gọi thủ công đến một trong các phương pháp update().