标签:
实现效果:将DatePicker和TimePicker修改的日期和时间实时显示在程序标题栏上。
1.通过DatePicker和TimePicker来实现
布局为main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <DatePicker android:id="@+id/datePicker" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
import java.util.Calendar; import android.support.v7.app.ActionBarActivity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MainActivity extends ActionBarActivity { private TimePicker timePicker; private DatePicker datePicker; private Calendar cal; private int year, month, day; private int hour, minute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取日历的一个对象 cal = Calendar.getInstance(); //获取年月日时分秒的信息 year = cal.get(Calendar.YEAR); //month从0开始计算(一月month = 0) month = cal.get(Calendar.MONTH)+1; day = cal.get(Calendar.DAY_OF_MONTH); hour = cal.get(Calendar.HOUR_OF_DAY); minute = cal.get(Calendar.MINUTE); //设置标题 setTitle(year+"-"+month+"-"+day+" "+hour+":"+minute); datePicker = (DatePicker) findViewById(R.id.datePicker); timePicker = (TimePicker) findViewById(R.id.timePicker); //datePicker的初始化 datePicker.init(year, cal.get(Calendar.MONTH), day, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) { //monthOfYear从0计算 setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); } }); timePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { setTitle(hourOfDay+":"+minute); } });
}
}
首先建立Calendar对象来获取当前的系统时间和日期,然后设置在程序标题栏上,注意cal.get(Calendar.MONTH)返回的值从0开始,即返回0时代表一月。
DatePicker初始化调用public void init(int year, int monthOfYear, int dayOfMonth, OnDateChangedListener onDateChangedListener),最后一个参数为DatePicker的监听器。TimePicker直接设置监听器。
运行截图
更改DatePicker
更改TimePicker
2.使用Dialog,更加美观
布局为main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
没有加入任何控件
MainActivity.java
import java.util.Calendar; import android.support.v7.app.ActionBarActivity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MainActivity extends ActionBarActivity {private Calendar cal; private int year, month, day; private int hour, minute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取日历的一个对象 cal = Calendar.getInstance(); //获取年月日时分秒的信息 year = cal.get(Calendar.YEAR); //month从0开始计算(一月month = 0) month = cal.get(Calendar.MONTH)+1; day = cal.get(Calendar.DAY_OF_MONTH); hour = cal.get(Calendar.HOUR_OF_DAY); minute = cal.get(Calendar.MINUTE); //设置标题 setTitle(year+"-"+month+"-"+day+" "+hour+":"+minute); //日历选择器的对话框形式 new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); } },year, cal.get(Calendar.MONTH), day).show(); //时间选择器的对话框形式(最后一个参数is24HourView,是否为24小时制) new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { setTitle(hourOfDay+":"+minute); } }, hour, minute, true).show(); } }
使用匿名类的方式,直接创建DatePickerDialog和TimePickerDialog对象,构造方法如下
public DatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
public TimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)
直接在构造方法中使用匿名内部类的方法添加监听器,is24HourView是个boolean类型的值,来表示是否使用24小时制。
运行截图
总结:
注意使用cal.get(Calendar.MONTH)得到的值会比真实的月份值小1,在setTitle时需要加1,而在传入到DatePicker的初始化方法和DatePickerDialog的构造方法时应该直接传入。
建议在这两个方法内直接写cal.get(Calendar.MONTH),不要用自定义变量month。
Android中DatePicker与TimePicker用法讲解(包括DatePickerDialog与TimePickerDialog)
标签:
原文地址:http://www.cnblogs.com/superkrissV/p/DatePickerTimePicker.html