当用户从片段中的DatePicker对话框中选择日期时,对话框和片段将被解除。我希望能够通知原始活动,然后它可以运行吐司。我宁愿不向片段添加代码,因为它已经在项目的许多其他地方使用。
当前代码的问题是在创建片段对话框(DatePicker)后立即运行toast。关于如何解决的任何想法?
活动文件:
...
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
Toast saveToast = Toast.makeText(this, "Now Click the Save Button", Toast.LENGTH_LONG);
saveToast.show();
答案 0 :(得分:1)
在你的片段实例中添加这样的代码......
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setCallBack(ondate);
newFragment.show(getSupportFragmentManager(), "datePicker");
并将onDate初始化为
DatePickerDialog.OnDateSetListener ondate = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker dp, int selectedYear,int selectedMonth, int selectedDay)
{
Calendar c = Calendar.getInstance();
c.set(selectedYear, selectedMonth, selectedDay);
String strDate = formatter.format(new Date(c.getTimeInMillis()));
// here you can add toast or any thing
}
};