运行对话框关闭后的一个函数

Run a function after dialog box is closed

我有一个按以下方式显示的自定义对话框:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dict_add_word_dialog_box);
    ok = findViewById(R.id.dictDialog_confirmButton);
    cancel = (Button) findViewById(R.id.dictDialog_cancelButton);
    ok.setOnClickListener(this);
    cancel.setOnClickListener(this);
}

点击浮动操作按钮时显示,通过:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this);
            customDialog.show();
            refreshRecyclerView();
        }
    });

我想要 refreshRecyclerView();仅在用户按下对话框上的“确定”按钮后才变为 运行。我该怎么做?

此外,只有当用户按下确定而不是取消时,我如何才能运行启用它?

创建一个包含您的 refreshRecyclerView 方法的可运行对象:

Runnable r = new Runnable() {
        @Override
        public void run() {
            refreshRecyclerView();
        }
    }

然后为该可运行对象创建一个处理程序:

Handler handler = new Handler();

在 ok 按钮的 onClickListener 中,通过调用以下命令触发可运行对象:

handler.post(r);

为什么不向您的自定义对话框添加侦听器?

var listener: Listener

public interface Listener {
    void onPositiveActionPressed();
}

然后在您的自定义对话框的 ok.onClickListener 中,您将拥有以下内容;

if(listener != null) {
    listener.onPositiveActionPressed();
}

最后;

DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this)
customDialog.listener = self

当然已经实施了DictCustomDialogBoxClass.Listener