Android控件之TimePicker、DatePicker
2015-12-05 16:31
441 查看
1、TimePicker、DatePicker简介
在前两篇文章中,使用的是弹出框的形式用来,展示时间和日期,而TimePicker、DatePicker控件则是将时间和日期显示在布局上。效果如下
2、代码
这样的时间和日期控件就需要在xml布局文件中,编写这两个控件相关的布局代码,使用TimePicker、DatePicker标签。具体如下
在前两篇文章中,使用的是弹出框的形式用来,展示时间和日期,而TimePicker、DatePicker控件则是将时间和日期显示在布局上。效果如下
2、代码
这样的时间和日期控件就需要在xml布局文件中,编写这两个控件相关的布局代码,使用TimePicker、DatePicker标签。具体如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.layout_timepicker.MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/timePicker1" android:layout_below="@+id/timePicker1" android:onClick="setTime" android:text="Button" /> <DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/datePicker1" android:layout_below="@+id/datePicker1" android:onClick="setDate" android:text="Button" /> </RelativeLayout> </ScrollView> </LinearLayout>java代码实现了,点击按钮后,弹出相应的时间设置,java代码如下
package com.example.layout_timepicker; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private TimePicker timePicker; private DatePicker datePicker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timePicker = (TimePicker) findViewById(R.id.timePicker1); datePicker = (DatePicker) findViewById(R.id.datePicker1); } public void setTime(View view){ int hour = timePicker.getCurrentHour(); int minute = timePicker.getCurrentMinute(); Toast.makeText(this, hour + ":" + minute, Toast.LENGTH_SHORT).show(); } public void setDate(View view){ int year = datePicker.getYear(); int month = datePicker.getMonth(); int day = datePicker.getDayOfMonth(); Toast.makeText(this, year + "年" + month + "月" + day + "日", Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
相关文章推荐
- Android SDK 开发范例大全 ---3.13置换文字颜色的机关
- android 自定义无限循环播放的viewPager。轮播ViewPager。实现循环播放 广告,主题内容,活动,新闻内容时。
- android studio 用git把项目推到bitbucket仓库
- Android 学习路线(参考)
- Android测试教程12--模拟读取的文件/数据库
- Android广播动态注册重复注册问题
- Android5.0推出的替换listView的控件recyclerview
- android makefile文件(Android.mk)组织结构解析
- android 世上actionbra最详细的介绍
- Android统计图表MPAndroidChart:为多条统计折线动态更新数据,以高温低温曲线为例【7】
- 释放Android的函数式能量(I):Kotlin语言的Lambda表达式
- Android开发者文档笔记(一)
- Android中Bitmap和Drawable
- android(24)(用java实现多线程断点下载)
- android下面的文案重用
- Android 布局 LinearLayout与RelativeLayout的布局属性
- ListView实现公告切换效果
- Android推送通知
- android自定义ProgressBar 修改进度条样式 出现setProgress无效问题
- android电话监听实现