0

我制作了 TimePicker Dialog 和 DatePicker Dialog:

时间选择器对话框:

public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}

日期选择器对话框:

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {


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);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}

我的问题是,当我选择时间或日期时,我想将其保存在特定的文本字段中。我该怎么做?

4

1 回答 1

0

你可以设置它的回调方法onDateSet/onTimeSet。

如果通过 textField,您的意思是 xml 布局中的 textView/EditText。

((TextView)findViewById(R.id.myTextField)).setText(FORMATTED_DATE);

对于格式化,您可以执行以下操作

String.format("%d-%d-%d",day,month,year)

类似的东西

于 2013-06-05T12:43:31.333 回答