本篇文章介绍DatePicke和DatePickerDialog的使用方法。
DatePicker实例:
public class MainActivity extends Activity {
//声明全局变量的引用
private DatePicker datepicker;
private TextView tv;
int mYear;
int mMonth;
int mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datepicker = (DatePicker) findViewById(R.id.dp);
//初始化日历对象
final Calendar c = Calendar.getInstance();
//得到当前日历的年月日
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
//通过init方法监听日期改变事件
datepicker.init(mYear, mMonth, mDay, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
Toast.makeText(MainActivity.this,"现在时间是" + year + "-" + (pad(monthOfYear + 1)) +"-"+(pad(dayOfMonth)), Toast.LENGTH_SHORT).show();
}
});
}
//对日期显示方式的调整,让日期以xxxx-xx-xx显示
private static String pad(int c){
if(c>=10){
return String.valueOf(c);
}
else{
return "0"+String.valueOf(c);
}
}
}
效果图:
效果图:设置时间为2014-05-01
在布局文件中设置DatePick的 android:calendarViewShown="false"属性可以不显示日历
<DatePicker
android:calendarViewShown="false"
android:id="@+id/dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
DatePickerDialog实例:
public class MainActivity extends Activity {
private Button bt;
private TextView tv;
int mYear;
int mMonth;
int mDay;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
bt=(Button)findViewById(R.id.bt);
Calendar c=Calendar.getInstance();
//获取当前日期Date对象
Date mdate=new Date();
//为Calendar对象设置时间为当前日期
c.setTime(mdate);
mYear=c.get(Calendar.YEAR);
mMonth=c.get(Calendar.MONTH);
mDay=c.get(Calendar.DAY_OF_MONTH);
tv.setText("当前日期:"+mYear+"-"+(pad(mMonth+1))+"-"+(pad(mDay)));
bt.setOnClickListener(new OnClickListener() {
/*创建DatePickerDialog对象,并显示出来
* 构造函数原型:
* public DatePickerDialog (Context context, DatePickerDialog.OnDateSetListener callBack,
* int year, int monthOfYear, int dayOfMonth)
* content组件运行Activity,
* DatePickerDialog.OnDateSetListener:选择日期监听事件
* year:当前组件上显示的年
* monthOfYear:当前组件上显示的月
*dayOfMonth:当前组件上显示的第几天
*
*/
public void onClick(View v) {
DatePickerDialog dpd=new DatePickerDialog(MainActivity.this,datelistener,mYear,mMonth,mDay);
dpd.show();
}
});
}
//创建DatePickerDialog监听事件,实现onDateSet方法
private DatePickerDialog.OnDateSetListener datelistener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
mYear=year;
mMonth=monthOfYear;
mDay=dayOfMonth;
updateDate();
}
};
protected void updateDate() {
tv.setText("当前日期:"+mYear+"-"+(pad(mMonth+1))+"-"+( pad (mDay)));
Toast.makeText(MainActivity.this, "当前日期:"+mYear+"-"+(pad(mMonth+1))+"-"+(pad(mDay)), Toast.LENGTH_SHORT).show();
}
private static String pad(int c){
if(c>=10){
return String.valueOf(c);
}
else{
return "0"+String.valueOf(c);
}
}
}
效果图:效果图:点击设置时间,设置为2014-05-01
效果图:设置完成后