onDateSet() 未被调用
onDateSet() is not called
查看了类似问题的答案,但不知道如何将它们应用到我的代码中。互联网上有关于此功能的信息,但可能是因为 Android 代码与其他代码相比太麻烦了(根据我的经验),我很难找到问题所在。
日历片段:
public class CalendarFragment extends DialogFragment{
@RequiresApi(api = Build.VERSION_CODES.N)
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener)getTargetFragment(),
year, month, day);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_calendar, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
日期片段:
public class DatesFragment extends Fragment implements DatePickerDialog.OnDateSetListener {
String selectedDate;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dates, container, false);
showCalendar = view.findViewById(R.id.calendar_button);
showCalendar.setOnClickListener(v -> {
DialogFragment calendarFragment = new CalendarFragment();
calendarFragment.show(getChildFragmentManager(), "calendar");
// ...
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
selectedDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
}
谢谢!
@laalto 提出了我在调用 getTargetFragment() 之前没有编写 setTargetFragment() 方法的可能性。情况确实如此,在添加 setTargetFragment() 之后调用了 onDateSet()。
查看了类似问题的答案,但不知道如何将它们应用到我的代码中。互联网上有关于此功能的信息,但可能是因为 Android 代码与其他代码相比太麻烦了(根据我的经验),我很难找到问题所在。
日历片段:
public class CalendarFragment extends DialogFragment{
@RequiresApi(api = Build.VERSION_CODES.N)
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener)getTargetFragment(),
year, month, day);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_calendar, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
日期片段:
public class DatesFragment extends Fragment implements DatePickerDialog.OnDateSetListener {
String selectedDate;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dates, container, false);
showCalendar = view.findViewById(R.id.calendar_button);
showCalendar.setOnClickListener(v -> {
DialogFragment calendarFragment = new CalendarFragment();
calendarFragment.show(getChildFragmentManager(), "calendar");
// ...
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
selectedDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
}
谢谢!
@laalto 提出了我在调用 getTargetFragment() 之前没有编写 setTargetFragment() 方法的可能性。情况确实如此,在添加 setTargetFragment() 之后调用了 onDateSet()。