android设置日期时间格式,Android中日期与时间设置控件的使用

1、日期设置控件:DatePickerDialog

2、时间设置控件:TimePickerDialog

实例代码

1、页面添加两个Button,单击分别显示日期设置控件和时间设置控件,还是有TextView控件,用于显示设置后的系统时间<?xml  version="1.0" encoding="utf-8"?>

Android:orientation="vertical"

Android:layout_width="fill_parent"

Android:layout_height="fill_parent"

>

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="@string/hello"

/>

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="Set the Date">

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="Set the Time">

package yyl.Android;

import java.text.DateFormat;

import java.util.Calendar;

import java.util.Locale;

import Android.app.Activity;

import Android.app.DatePickerDialog;

import Android.app.TimePickerDialog;

import Android.os.Bundle;

import Android.view.View;

import Android.widget.Button;

import Android.widget.DatePicker;

import Android.widget.TextView;

import Android.widget.TimePicker;

public class ChronoDemo extends Activity {

//获取日期格式器对象

DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();

//定义一个TextView控件对象

TextView dateAndTimeLabel = null;

//获取一个日历对象

Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);

//当点击DatePickerDialog控件的设置按钮时,调用该方法

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener()

{

@Override

public void onDateSet(DatePicker view, int year, int monthOfYear,

int dayOfMonth) {

//修改日历控件的年,月,日

//这里的year,monthOfYear,dayOfMonth的值与DatePickerDialog控件设置的最新值一致

dateAndTime.set(Calendar.YEAR, year);

dateAndTime.set(Calendar.MONTH, monthOfYear);

dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);

//将页面TextView的显示更新为最新时间

updateLabel();

}

};

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {

//同DatePickerDialog控件

@Override

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);

dateAndTime.set(Calendar.MINUTE, minute);

updateLabel();

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//得到页面设定日期的按钮控件对象

Button dateBtn = (Button)findViewById(R.id.setDate);

//设置按钮的点击事件监听器

dateBtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//生成一个DatePickerDialog对象,并显示。显示的DatePickerDialog控件可以选择年月日,并设置

new DatePickerDialog(ChronoDemo.this,

d,

dateAndTime.get(Calendar.YEAR),

dateAndTime.get(Calendar.MONTH),

dateAndTime.get(Calendar.DAY_OF_MONTH)).show();

}

});

Button timeBtn = (Button)findViewById(R.id.setTime);

timeBtn.setOnClickListener(new View.OnClickListener() {

//同上原理

@Override

public void onClick(View v) {

new TimePickerDialog(ChronoDemo.this,

t,

dateAndTime.get(Calendar.HOUR_OF_DAY),

dateAndTime.get(Calendar.MINUTE),

true).show();

}

});

dateAndTimeLabel=(TextView)findViewById(R.id.dateAndTime);

updateLabel();

}

//更新页面TextView的方法

private void updateLabel() {

dateAndTimeLabel.setText(fmtDateAndTime

.format(dateAndTime.getTime()));

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值