DataPicker以及TimePicker显示时间和日期(屏幕上显示)

2017-08-22 09:03 出处:360java.com 作者:360java  阅读()
public class MainActivity extends Activity { private DatePicker date_picker; private TimePicker time_picker; private TextView tv_date; private TextView tv_time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate

public class MainActivity extends Activity {
private DatePicker date_picker;
private TimePicker time_picker;
private TextView tv_date;
private TextView tv_time;
@Override
   protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化
date_picker=(DatePicker) findViewById(R.id.date);
time_picker=(TimePicker) findViewById(R.id.time);
tv_date=(TextView) findViewById(R.id.tv_date);
tv_time=(TextView) findViewById(R.id.tv_time);
//获取当前的年月日时分信息
Calendar c=Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DAY_OF_MONTH);
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
//使用Calendar显示时,月份从0开始,显示的时候加1
tv_date.setText("现在日期:"+year+"-"+(month+1)+"-"+day);
tv_time.setText("现在时间:"+hour+":"+minute);
 //初始化日期时候,需要日期监听器
date_picker.init(year, month, day, new OnDateChangedListener() ;{
//监听日期的改变
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
//Toast.makeText(MainActivity.this, "现在时间:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth, 0).show();
//月份以0开始统计,显示的时候要加1
tv_date.setText("现在日期:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
}
});
//设置时间为24小时制
time_picker.setIs24HourView(true);
//时间监听器
time_picker.setOnTimeChangedListener(new OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
tv_time.setText("现在时间:"+hourOfDay+":"+minute);
}
});
分享到:
本文标签: DataPicker, 以及, TimePicker, 显示, 时间

相关文章

发表评论(共条评论)愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

Copyright (C) java学习 360java 360java.com, All Rights Reserved.

苏ICP备16022210号