简体   繁体   English

Android:强制重新建立对话

[英]Android: Force recreation of dialog

I am using the android dateslider custom dialog class in order to let the user edit the date for several different rows of a table. 我正在使用android datelider自定义对话框类,以便让用户编辑表中几行的日期。

The dateslider lets you limit the user to only select dates between a minimum date and maximum date that you can specify. 使用dateslider可以限制用户仅选择可以指定的最小日期和最大日期之间的日期。

Each table row requires the dateslider to limit the user to a different minimum date and maximum date, however because you specify the min and max dates inside the onCreateDialog method, I need to be able to dynamically modify these dates when the user clicks the row. 每个表行都需要dateslider来将用户限制为不同的最小日期和最大日期,但是,由于您在onCreateDialog方法中指定了最小和最大日期,因此我需要能够在用户单击该行时动态修改这些日期。

I have tried calling the onCreateDialog method again when the user clicks the dialog, and it is ran, however the new limits are not taken into account, suggesting that the originally created dialog is still used instead. 我试过当用户单击对话框时再次调用onCreateDialog方法,并且该对话框已运行,但是未考虑新的限制,建议仍使用原始创建的对话框。

How would I go about achieving my goal? 我将如何实现自己的目标?

Thanks, Max. 谢谢,马克斯。

If you need to change dialogs before you use them, you need to use onPrepareDialog . 如果需要在使用前更改对话框,则需要使用onPrepareDialog

Update: 更新:

The dialog that is passed in to onPrepareDialog is the dialog that was created in onCreateDialog . 传递给onPrepareDialog的对话框是在onCreateDialog中创建的对话框。 Modify it however you like (don't create a new one). 修改它,但是您喜欢(不要创建一个新的)。 You might have to add some setters to your custom dialog class: 您可能必须向自定义对话框类中添加一些设置器:

protected void onPrepareDialog(int id, Dialog dialog) {
    switch(id) {
    case YOUR_DIALOG_ID:
        YearMonthDayHourMinute myDialog = (YearMonthDayHourMinute) dialog;
        myDialog.setInitialTime(initialTime);
        myDialog.setMinTime(minTime);
        myDialog.setMaxTime(maxTime);
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM