1、日期设置控件:DatePickerDialog

2、时间设置控件:TimePickerDialog

实例代码

1、页面添加两个Button,单击分别显示日期设置控件和时间设置控件,还是有TextView控件,用于显示设置后的系统时间

1. [代码]main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
Android:orientation="vertical"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
>
<TextView Android:id="@+id/dateAndTime"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="@string/hello"
/>
<Button Android:id="@+id/setDate"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="Set the Date"></Button>
<Button Android:id="@+id/setTime"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="Set the Time"></Button>
</LinearLayout>

2. [代码]ChronoDemo.java

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()));
}
}

来自:
源码天堂

最新文章

  1. python中深复制与浅复制
  2. 设计模式学习笔记-Adapter模式
  3. RealProxy深入
  4. 彻底理解ThreadLocal一
  5. SQL Sever2008r2 数据库服务各种无法启动的解决办法
  6. Git使用简介
  7. CentOS 6.4 使用YUM 安装MySQL5.5
  8. HBase API详解
  9. PHP冒泡排序法
  10. cocos2d-x-2.2.0_win7+vs2010
  11. Flash Builder4.0新建Flex应用程序切换主题出错
  12. jsapi 调起微信支付的的踩坑
  13. BSGS与exBSGS学习笔记
  14. [Luogu 3787] 冰精冻西瓜
  15. [2017BUAA软工]第零次作业
  16. topcoder srm 300 div1
  17. 如何对CentOS FTP服务配置
  18. Nginx 防盗链
  19. T-SQL 之 公用表表达式(CTE)
  20. iOS安全系列之 HTTPS

热门文章

  1. python 序列类型
  2. 利用Azure Automation实现云端自动化运维(2)
  3. 谈谈我对Java中CallBack的理解
  4. android关于window
  5. iOS 开发者必知的 75 个工具
  6. Node.js log1: ERR can not find module express
  7. pyspark 写 logistic regression
  8. 函数dirname--返回路径中的目录部分
  9. HDU2594——Simpsons’ Hidden Talents
  10. IKAnalyzer使用停用词词典进行分词