TimePicker和DatePicker的 [英] TimePicker and DatePicker

查看:226
本文介绍了TimePicker和DatePicker的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经TimePicker对话和对话的DatePicker:

I have made TimePicker Dialog and DatePicker Dialog:

TimePicker对话框:

TimePicker 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
}
}

DatePicker的对话框:

DatePicker Dialog:

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

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

My problem is that when I pick time or date I want to save it in specific textField. How can I do it?

推荐答案

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

You can set it callback method onDateSet/onTimeSet.

如果通过文本框,你的意思的TextView / EditText上你的XML布局。

If by textField, you mean textView/EditText in your xml layout.

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

有关格式,你可以做这样的事情。

For formatting, you can do something like this

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

东西艾克说

这篇关于TimePicker和DatePicker的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆