首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TimePicker和DatePicker

TimePicker和DatePicker
EN

Stack Overflow用户
提问于 2013-06-05 20:39:20
回答 1查看 421关注 0票数 0

我制作了TimePicker对话框和DatePicker对话框:

TimePicker对话框:

代码语言:javascript
复制
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
}
}

DatePicker对话框:

代码语言:javascript
复制
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
}
}

我的问题是,当我选择时间或日期时,我希望将其保存在特定的textField中。我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2013-06-05 20:43:31

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

如果您所说的textField是指xml布局中的textView/EditText。

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

对于格式化,您可以这样做

代码语言:javascript
复制
String.format("%d-%d-%d",day,month,year)

诸如此类的东西

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16940181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档