2011-12-06 32 views
57

Tôi đang sử dụng DialogFragment và trong khi tôi đã đặt thành công hình ảnh để đóng (ví dụ: bỏ qua) hộp thoại khi được nhấn, tôi đang gặp khó khăn khi tìm cách loại bỏ hộp thoại khi người dùng nhấp vào bất kỳ nơi nào bên ngoài nó, giống như nó hoạt động với các hộp thoại bình thường. Tôi nghĩ sẽ có một số loại số điện thoạiLàm thế nào để loại bỏ một DialogFragment khi nhấn bên ngoài hộp thoại?

dialogFragment.setCanceledOnTouchOutside(true); 

nhưng tôi không thấy điều đó trong tài liệu.

Điều này có thể thực hiện được với DialogFragment không? Hay tôi đang tìm kiếm địa điểm sai? Tôi đã cố gắng ngăn chặn các sự kiện liên lạc trong hoạt động 'cha mẹ' nhưng ngoài việc không nhận được bất kỳ sự kiện liên lạc nào, điều đó dường như không đúng với tôi.

Trả lời

136
DialogFragment.getDialog().setCanceledOnTouchOutside(true); 

Phải được gọi trong onCreateView (như Apurv Gupta đã chỉ ra).

+26

Phải được gọi trong 'onCreateView' –

+0

Điều gì sẽ xảy ra nếu tôi không muốn hủy nhưng thay vào đó chỉ đóng? – jjxtra

50
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     ... 
     getDialog().setCanceledOnTouchOutside(true); 
     ... 
     } 
1
DialogFragment.getDialog().setCanceledOnTouchOutside(false); 

Nó được đánh nhầm. Tôi đã từng gặp vấn đề tương tự. Đây hoạt động tốt cho Java và Mono for android Mono sẽ là:

this.getDialog().SetCanceledOnTouchOutside(false); 
18
/** The system calls this only when creating the layout in a dialog. */ 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // The only reason you might override this method when using onCreateView() is 
     // to modify any dialog characteristics. For example, the dialog includes a 
     // title by default, but your custom layout might not need it. So here you can 
     // remove the dialog title, but you must call the superclass to get the Dialog. 
     Dialog dialog = super.onCreateDialog(savedInstanceState); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setCanceledOnTouchOutside(true); 

     return dialog; 
    } 
+0

điều này không hiệu quả đối với tôi. Tôi đã phải gọi 'setCanceledOnTouchOutside' trong' onCreateView' theo @Apurv. Tôi nên đề cập đến rằng tôi gọi là 'setCanceledOnTouchOutside (false) ' – kimbaudi

0

tôi sẽ khuyên bạn nên sử dụng giải pháp của tôi chỉ sau khi thử các giải pháp trên. Tôi đã mô tả giải pháp của tôi here. Nói tóm lại, tôi đang kiểm tra các ràng buộc của DialogFragment.getView(). Khi các điểm tiếp xúc nằm ngoài DialogFragment, tôi đang loại bỏ Hộp thoại.

0
  Dialog.SetCanceledOnTouchOutside(true); 

Làm việc đối với tôi
Mã My

class dlgRegister : DialogFragment 
     { 
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      { 
    .... 
    .... 
    } 
    public override void OnActivityCreated(Bundle savedInstanceState) 
      { 
       Dialog.Window.RequestFeature(WindowFeatures.NoTitle); 
       Dialog.SetCanceledOnTouchOutside(true); 
       base.OnActivityCreated(savedInstanceState); 
       Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation; 
      } 
    } 
+0

Bạn đang đóng nhưng không đúng! – sud007

2

lô các câu trả lời ở đây nhưng, sự sụp đổ của ứng dụng khi hộp thoại sẽ mở ra. Viết getDialog().setCanceledOnTouchOutside(true); bên trong onCreateView không hoạt động và đã làm hỏng ứng dụng của tôi.

(Tôi đang sử dụng AppCompatActivity làm BaseActivity và android.app.DialogFragment làm Phân đoạn của mình).

Điều gì làm việc là một trong hai dòng sau:

getDialog() setCanceledOnTouchOutside (true);.

HOẶC

this.getDialog() setCanceledOnTouchOutside (true).

bên onActivityCreated như

@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     //getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationZoom; 
     //getDialog().getWindow().setDimAmount(0.85f); 
     getDialog().setCanceledOnTouchOutside(true);//See here is the code 
    } 

gì không sử dụng:

DialogFragment.getDialog().setCanceledOnTouchOutside (sai);

ném lỗi sau

enter image description here

Và viết code trong onCreateView vụ va chạm App! Vui lòng cập nhật câu trả lời nếu bạn tìm thấy điều gì đó sai.

Các vấn đề liên quan