2011-02-06 29 views
9

我正在開發一個小程序,我需要添加一個自定義對話框,它在關閉時將一些信息傳遞給調用活動性。 我擴展了對話框類,當我嘗試在關閉時捕獲自定義對話框時,使用onDismiss偵聽器,它永遠不會觸及它,因爲我使用了自定義對話框。使用自定義對話框時無法使用onDismiss() - Android

這是我的活動的一部分 -

. 
    . 
    . 
     attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition()); 
     customizeDialog.show(); 

(屬性是一個擴展的對話框類的類的名稱)。

這裏是事件監聽器我成立時的對話結束 -

customizeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 

     @Override 
     public void onDismiss(DialogInterface dialog) { 
      Log.v("LOG_CAT",attributes.selectedIndexes.get(0) + " " + attributes.selectedIndexes.get(1) + " " + attributes.selectedIndexes.get(2) + " " + attributes.selectedIndexes.get(3) + " " + attributes.selectedIndexes.get(5) + " "); 
    } 

}); 

我知道我做錯了,我只是不知道如何解決它。

我真的很感謝這個問題的任何幫助。

謝謝!

+1

您是否缺少對自定義對話框類中的某個超級方法的調用?這是常見的罪魁禍首,當你在一個自定義類期待的事件沒有發生。 – 2011-02-06 22:35:06

+0

Hey Tofira我使用了相同的代碼,它使用得很好。也許你錯過了別的東西? – 2011-07-10 05:01:36

回答

16

我傾向於有我的活動實現這樣的聽衆......

public class MyActivity extends Activity 
    implements DialogInterface.OnDismissListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition()); 
     customizeDialog.setOnDismissListener(this); 
     customizeDialog.show(); 
    } 

    @Override 
    public void onDismiss(DialogInterface dialog) { 
     // Do whatever 
    } 
} 
5

你可以有你的電話活動實現自定義的監聽器接口時調用的對話框關閉:

public interface MyDialogListener { 
    void OnCloseDialog(); 
} 

public class MyActivity implements MyDialogListener { 
    public void SomeMethod() { 
     MyDialog myDialog = new MyDialog(this, this); 
     myDialog.show(); 
    } 

    public void OnCloseDialog() { 
     // Do whatever you want to do on close here 
    } 

} 

public class MyDialog extends Dialog { 
    MyDialogListener mListener; 

    public MyDialog (Context context, MyDialogListener listener) { 
     super(context, R.style.Dialog); 
     mListener = listener; 
    } 

    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.CloseButton: 
       mListener.OnCloseDialog(); 
       dismiss() 
       break; 
      default: 
       //... 
     } 
    } 
} 

如果您想在除解僱之外的任何其他時間將內容發送回主叫方,此功能尤其有用。

0

有一點要記住的是,OnDismissListener正在傾聽對子進程的解僱。您客戶對話的父母需要onDismissListener,而不是對話本身。

「用於允許對話創建者在對話被解除時運行一些代碼的接口。」

1

如果你想擁有某種形式的對話裏面保存的,同樣,你必須使用自定製對話框onDicmissListeneronDismiss默認是不叫:

public class CustomDialog extends Dialog implements DialogInterface.OnDismissListener { 

    public CustomDialog(Context context) { 
     super(context); 
     setupLayout(context); 
    } 

    public CustomDialog(Context context, int theme) { 
     super(context, theme); 
     setupLayout(context); 
    } 

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { 
     super(context, cancelable, cancelListener); 
     setupLayout(context); 
    } 

    private void setupLayout(Context context) { 
     this.context = context; 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.custom_dialog); 
     WindowManager.LayoutParams params = getWindow().getAttributes(); 
     params.width = WindowManager.LayoutParams.FILL_PARENT; 
     getWindow().setAttributes(params); 

     setOnDismissListener(this); 

     loadPreferences(); 
    } 

    private void loadPreferences() { 
     // ... 
    } 

    private void savePreferences() { 
     // ... 
    } 

    @Override 
    public void onDismiss(DialogInterface dialogInterface) { 
     savePreferences(); 
    } 
} 
0

要添加CustomDialog類的內部對話:

public class MessageBoxDialog extends Dialog implements DialogInterface.OnDismissListener 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     ... 
     setOnDismissListener(this); 
     ... 
    } 

    @Override 
    public void onDismiss(DialogInterface dialogInterface) { 

    } 
} 
相關問題