主要方法
public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
setOnTimeChangedListener public void setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)
本文主要实现一个DatePicker和TimePicker组合使用的时间选择器,为后续短信的定时发送时间做伏笔。
话不多说,上代码。
先看main.xml文件
<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/dateButton" android:layout_width="match_parent" android:layout_height="40dip" android:layout_marginTop="20dip" android:text="year-month-day" android:textSize="18dip" android:background="@drawable/bg" android:onClick="pickSendDate" /> <Button android:id="@+id/timeButton" android:layout_below="@id/dateButton" android:layout_width="match_parent" android:layout_height="40dip" android:layout_marginTop="1dip" android:text="hour:minute" android:textSize="18dip" android:background="@drawable/bg" android:onClick="pickSendTime" /> <DatePicker android:id="@+id/datePicker" android:layout_below="@id/timeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:startYear="2014" android:endYear="2020" android:visibility="gone" /> <TimePicker android:id="@+id/timePicker" android:layout_below="@id/timeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:visibility="gone" /> </RelativeLayout>
整体采用相对布局,注意要想使控件水平居中时使用 android:layout_centerHorizontal="true"。而非android:layout_gravity="center_vertical"(LinearLayout布局下此种方式才有效)。DatePicker可以设置最小和最大年限,十分方便。对于Button控件的监听没有采用代码中setOnclickButtonListener,直接使用android:onClick,后边跟方法名。(两种方式均可)
代码中存在硬编码,实际项目中要注意,这里请无视。
主界面MainActivity
MainActivity.java
package com.beny.pickdate; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener;//监听日期变化 import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener;//监听时间变化 public class MainActivity extends Activity { private int year; private int month; private int day; private int hour; private int minute; private TimePicker timePicker = null; private DatePicker datePicker = null; private Button timeButton = null; private Button dateButton = null; protected String[] time = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timeButton = (Button)findViewById(R.id.timeButton); dateButton = (Button)findViewById(R.id.dateButton); timePicker = (TimePicker)findViewById(R.id.timePicker); timePicker.setIs24HourView(true); //设定默认时间为当前系统时间 /*也可以用TimePicker的getCurrentHour()和getCurrentMinute()方法 获得当前时间*/ time = getCurrentTime(); timePicker.setCurrentHour(Integer.valueOf(time[0])); timePicker.setCurrentMinute(Integer.valueOf(time[1])); //设置日期 datePicker = (DatePicker)findViewById(R.id.datePicker); datePicker.setCalendarViewShown(false); //设置button上的默认日期时间 timeButton.setText(timePicker.getCurrentHour()+":"+timePicker.getCurrentMinute()); dateButton.setText(datePicker.getYear()+"年"+(datePicker.getMonth()+1)+"月"+datePicker.getDayOfMonth()+"日"); //设置日期选择器 默认可见 datePicker.setVisibility(View.VISIBLE); dateButton.setBackgroundResource(R.color.button_pressed_bg); //设置监听器 timePicker.setOnTimeChangedListener(new SendTimeChangedListener()); datePicker.init(datePicker.getYear(), datePicker.getMonth()+1, datePicker.getDayOfMonth(), new SendDateChangedListener()); } public String[] getCurrentTime(){ //取得当前的hour minute即可 SimpleDateFormat curTimeFormat = new SimpleDateFormat("HH-mm"); String curTime = curTimeFormat.format(new Date()); String[] time = new String[2]; time = curTime.split("-"); return time; } public String[] getCurrentDate(){ //取得当前日期的年月日 SimpleDateFormat curDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String curDate = curDateFormat.format(new Date()); String[] date = new String[3]; date = curDate.split("-"); return date; } public void pickSendDate(View view){ datePicker.setVisibility(View.VISIBLE); timePicker.setVisibility(View.INVISIBLE); timeButton.setBackgroundResource(R.color.button_bg); dateButton.setBackgroundResource(R.color.button_pressed_bg); } public void pickSendTime(View view){ datePicker.setVisibility(View.INVISIBLE); timePicker.setVisibility(View.VISIBLE); timeButton.setBackgroundResource(R.color.button_pressed_bg); dateButton.setBackgroundResource(R.color.button_bg); } //TODO //设定日期是否早于当前日期 private boolean isBeforeCurDate(){ return false; } //TODO //设定时间是否早于当前时间 private boolean isBeforeCurTime(){ return false; } class SendTimeChangedListener implements OnTimeChangedListener{ @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { timeButton.setText(hourOfDay+":"+minute); MainActivity.this.hour = hourOfDay; MainActivity.this.minute = minute; } } class SendDateChangedListener implements OnDateChangedListener{ @Override public void onDateChanged(DatePicker view, int year, int month, int day) { dateButton.setText(year+"年"+(month+1)+"月"+day+"日"); MainActivity.this.year = year; MainActivity.this.month = month; MainActivity.this.day = day; } } //创建菜单 @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; } }
有个要注意的地方,就是month月份要加1,否则月份不对。
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="button_pressed_bg">#DD5D04</color> <color name="button_bg">#F7F7F7</color> </resources>
bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@color/button_pressed_bg" /> <item android:drawable="@color/button_bg" /> </selector>
效果图
1.默认状态下效果图,默认选中日期按钮,显示DatePicker视图,默认加载系统当前日期。
2.点击时间按钮控件,视图切换到TimePicker,默认也是加载进入时系统时间。
3.上下选择日期或者时间,日期Button控件或时间Button控件中的文字信息会随之改变,这得益于给控件设置监听器后复写了其onDateChanged(DatePicker view, int year, int month, int day) 和onTimeChanged(TimePicker view, int hourOfDay, int minute)方法。