簡體   English   中英

使用自定義對話框時無法使用onDismiss() - Android

[英]Can't use onDismiss() when using custom dialogs - Android

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

這是我活動的一部分 -

    .
    .
    .
       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) + " ");
    }

});

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

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

謝謝!

我傾向於讓我的活動實現這樣的聽眾......

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
    }
}

您可以讓您的調用活動實現在對話框關閉時調用的自定義偵聽器接口:

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:
                //...
        }
    }
}

如果您想在解雇之外的任何其他時間將內容發送回呼叫者,這將特別有用。

如果你想在對話框中進行某種保存,那么你必須使用onDicmissListener因為默認情況下不調用onDismiss自定義對話框:

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();
    }
}

如果您使用的是自定義對話框但無法將其關閉,請嘗試以下代碼。 它對我有用。

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           dialog.dismiss();
        }
    }, 1500);

要記住的一件事是OnDismissListener正在偵聽子進程的解雇。 客戶對話框的父級需要onDismissListener ,而不是對話框本身。

“用於在對話框被解除時允許對話框的創建者運行某些代碼的接口。”

要在CustomDialog類中添加對話框:

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

    @Override
    public void onDismiss(DialogInterface dialogInterface) {

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM