欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 日期和时间的使用实例详解

程序员文章站 2024-02-25 18:55:51
android 日期和时间的使用 日期和时间的使用; 1:弹出框timepickerdialog,datepickerdialog 2:组件timepic...

android 日期和时间的使用

Android 日期和时间的使用实例详解

日期和时间的使用;

1:弹出框timepickerdialog,datepickerdialog

2:组件timepicker,datepicker

timepickerdialog的使用:通过点击button显示图一,然后用户可以设置时间

datepickerdialog的使用只需要将timepickerdialog修改成datepickerdialog, timepickerdialog.ontimesetlistener 分别修改成datepickerdialog,ondatesetlistener既可

public static 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
  }
}
 
  
 android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:text="@string/pick_time" 
  android:onclick="showtimepickerdialog" />
public void showtimepickerdialog(view v) {
  dialogfragment newfragment = new timepickerfragment();
  newfragment.show(getsupportfragmentmanager(), "timepicker");
}


datepickerdialog的代码:

public class datepickerfragment extends dialogfragment implements datepickerdialog.ondatesetlistener {

  //用户创建日期对话框的时间方法
  @override
  public dialog oncreatedialog(bundle savedinstancestate) {

    calendar c = calendar.getinstance();
    int year = c.get(calendar.year);
    int month = c.get(calendar.month);
    int day = c.get(calendar.day_of_month);
    datepickerdialog dialog = new datepickerdialog(getactivity(), this, year, month, day);
    return dialog;
  }



  @override
  public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) {



  }

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!