当前位置: 首页 > news >正文

【Android】时间与日期Widget(DatePicker 与 TimePicker)


public class

DatePicker

extends FrameLayout
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.FrameLayout
android.widget.DatePicker

Class Overview

This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or aCalendarView. The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed. Also the minimal and maximal date from which dates to be selected can be customized.

See theDate Picker tutorial.

To provide a widget for selecting a date, use theDatePickerwidget, which allows the user to select the month, day, and year, in a familiar interface.

In this tutorial, you'll create aDatePickerDialog, which presents the date picker in a floating dialog box at the press of a button. When the date is set by the user, aTextViewwill update with the new date.


public class

TimePicker

extends FrameLayout
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.FrameLayout
android.widget.TimePicker

Class Overview

A view for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners. The hour can be entered by keyboard input. Entering in two digit hours can be accomplished by hitting two digits within a timeout of about a second (e.g. '1' then '2' to select 12). The minutes can be entered by entering single digits. Under AM/PM mode, the user can hit 'a', 'A", 'p' or 'P' to pick. For a dialog using this view, seeTimePickerDialog.

See theTime Picker tutorial.



下面是一个转载自书上的示例:

public class Activity01 extends Activity
{
	TextView	m_TextView;
	//声明dataPicker
	DatePicker	m_DatePicker;
	//声明TimePicker
	TimePicker	m_TimePicker;
	Button 		m_dpButton;
	Button 		m_tpButton;
	//java中的Calendar类
	Calendar 	c;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		c=Calendar.getInstance();
		
		m_TextView= (TextView) findViewById(R.id.TextView01);
		m_dpButton = (Button)findViewById(R.id.button1);
		m_tpButton = (Button)findViewById(R.id.button2);
		
		//获取DataPicker对象
		m_DatePicker = (DatePicker) findViewById(R.id.DatePicker01);
		//初始化当前时间并设置监听
		m_DatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
			{
				//当日期更改在这里设置
				c.set(year, monthOfYear, dayOfMonth);
			}
		});

		//
		m_TimePicker = (TimePicker) findViewById(R.id.TimePicker01);
		//
		m_TimePicker.setIs24HourView(true);

		//
		m_TimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
			{
				//当时间更改在这里设置
				c.set(year, month, day, hourOfDay, minute, second);
			}
		});
		
		
		m_dpButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				new DatePickerDialog(Activity01.this,
					new DatePickerDialog.OnDateSetListener()
					{
						public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
						{
							//设置日历
						}
					},c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
			}
		});
		
		m_tpButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v)
			{
				new TimePickerDialog(Activity01.this,
					new TimePickerDialog.OnTimeSetListener()
					{
						public void onTimeSet(TimePicker view, int hourOfDay,int minute)
						{
							//设置时间
						}
					},c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();
			}
		});
	}
}


 

 
对应的layout文件
<?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/TextView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <Button
      android:id="@+id/Button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
    >
    </Button>
    <Button
      android:id="@+id/Button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
    >
    </Button>
</LinearLayout>


 


                

相关文章:

  • 表单
  • 面试题——二维数组中的查找
  • 使用java poi解析表格
  • 【Android】如何查看每个方法所花费的时间从而进行Performance的调优
  • docker-compose命令
  • 你还在迭代和递归吗?
  • 受欢迎的牛
  • appium自动化安装(一)
  • Template Method模板方法
  • UVA 10603 倒水问题
  • 程序员编程艺术第二十六章:基于给定的文档生成倒排索引(含源码下载)
  • Flume数据采集准备
  • 【Android】Menu不同菜单的使用介绍
  • python初识
  • Elasticsearch教程收集
  • php的引用
  • Java 多线程编程之:notify 和 wait 用法
  • Spring Cloud中负载均衡器概览
  • Vue.js-Day01
  • 爱情 北京女病人
  • 测试如何在敏捷团队中工作?
  • 多线程 start 和 run 方法到底有什么区别?
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 基于axios的vue插件,让http请求更简单
  • 坑!为什么View.startAnimation不起作用?
  • 浏览器缓存机制分析
  • 线上 python http server profile 实践
  • 译有关态射的一切
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • # Apache SeaTunnel 究竟是什么?
  • # 安徽锐锋科技IDMS系统简介
  • # 计算机视觉入门
  • $GOPATH/go.mod exists but should not goland
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (9)目标检测_SSD的原理
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (六)c52学习之旅-独立按键
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (一)Dubbo快速入门、介绍、使用
  • (转)winform之ListView
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET Core WebAPI中封装Swagger配置
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .NET实现之(自动更新)
  • [2015][note]基于薄向列液晶层的可调谐THz fishnet超材料快速开关——
  • [2018/11/18] Java数据结构(2) 简单排序 冒泡排序 选择排序 插入排序
  • [Android Pro] AndroidX重构和映射
  • [BZOJ1010] [HNOI2008] 玩具装箱toy (斜率优化)
  • [C++]模板与STL简介
  • [CC2642R1][VSCODE+Embedded IDE+IAR Build+Cortex-Debug] TI CC2642R1基于VsCode的开发环境
  • [Docker]三.Docker 部署nginx,以及映射端口,挂载数据卷