我有一个包含EditText的片段,当我点击EditText时,会出现一个DatePicker对话框来选择日期。
Fragment文件
public class FirstFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
EditText editText = (EditText) view.findViewById(R.id.visitDateFiled);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "Date Picker");
}
});
return view;
}
DialogFragment
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = sdf.format(c.getTime());
}
}
我不知道如何将数据(选定的日期)从DialogFragment传递到Fragment?
我阅读了一些主题,但对我没有帮助,我感到困惑(很抱歉再次提出这个问题)。