【问题标题】:TimePicker and DatePicker时间选择器和日期选择器
【发布时间】:2013-06-05 20:59:04
【问题描述】:

我已经制作了 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
}
}

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

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

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

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

    类似的东西

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多