gpt4 book ai didi

android - [Android]如何在Fragment中为AlertDialog设置final

转载 作者:行者123 更新时间:2023-11-29 15:09:07 24 4
gpt4 key购买 nike

当我触摸 fragment 中的编辑文本时,我想使用 AlertDialog 来显示日历。过去,当我使用 Activity 类时,它工作得很好。但是当我使用 Fragment 时,它不起作用。

这是在 Fragment 中触摸编辑文本时显示日历对话框的方法:

 private void setDateTimeField() {
try {
et_date.setOnClickListener(this);

final Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth - 1);
if (newDate.getTime().getTime() > (newCalendar.getTime().getTime())) {

final AlertDialog builder = new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage("This is a date of future! We will get current date for this review!").show();

//this code below is coppied in https://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/
final Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
builder.dismiss(); // when the task active then close the dialog
t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
}
}, 2000); // after 2 second (or 2000 miliseconds), the task will be active.
et_date.setText(dateFormatter.format(newCalendar.getTime()));

} else {
newDate.set(year, monthOfYear, dayOfMonth);
et_date.setText(dateFormatter.format(newDate.getTime()));
}
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
} catch (Exception ex) {
messages("Something Wrong!");
}
}

错在:

 final  AlertDialog builder = new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage("This is a date of future! We will get current date for this review!").show();

You can see this error here当我删除 final for builder 时,上面的代码没问题,但其他问题出现在:

builder.dismiss();

You can see this error here上面的代码要求构建器必须是最终的。我知道我的问题的最佳解决方案是使用 DialogFragment。但我不想使用它。此外,我想知道这个错误的原因。请帮我。p/s: 我的英语不好,如果我的语法有任何错误,我很抱歉,谢谢你阅读我的问题。

最佳答案

改变

fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

fromDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

关于android - [Android]如何在Fragment中为AlertDialog设置final,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33714175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com