为什么DatePicker控件不能和TimePicker控件同时使用

 我来答
大单不呵啦5914
2016-12-10 · 超过215用户采纳过TA的回答
知道小有建树答主
回答量:404
采纳率:100%
帮助的人:306万
展开全部
安卓里面的时间控件与日期控件都是分开的,如果要同时使用的话用户体验度那肯定不好,整合了安卓中的时间与日期控件,可以同时显示出来方便用户进行选择,使用时只需要将文件导入项目中,再需要用到的地方使用如下代码
[html] view plaincopyprint?
package com.andyidea.calenderdemo;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends Activity {

private EditText showDate = null;
private Button pickDate = null;
private EditText showTime = null;
private Button pickTime = null;

private static final int SHOW_DATAPICK = 0;
private static final int DATE_DIALOG_ID = 1;
private static final int SHOW_TIMEPICK = 2;
private static final int TIME_DIALOG_ID = 3;

private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

initializeViews();

final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

setDateTime();
setTimeOfDay();
}

/**
* 初始化控件和UI视图
*/
private void initializeViews(){
showDate = (EditText) findViewById(R.id.showdate);
pickDate = (Button) findViewById(R.id.pickdate);
showTime = (EditText)findViewById(R.id.showtime);
pickTime = (Button)findViewById(R.id.picktime);

pickDate.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Message msg = new Message();
if (pickDate.equals((Button) v)) {
msg.what = MainActivity.SHOW_DATAPICK;
}
MainActivity.this.dateandtimeHandler.sendMessage(msg);
}
});

pickTime.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Message msg = new Message();
if (pickTime.equals((Button) v)) {
msg.what = MainActivity.SHOW_TIMEPICK;
}
MainActivity.this.dateandtimeHandler.sendMessage(msg);
}
});
}

/**
* 设置日期
*/
private void setDateTime(){
final Calendar c = Calendar.getInstance();

mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

updateDateDisplay();
}

/**
* 更新日期显示
*/
private void updateDateDisplay(){
showDate.setText(new StringBuilder().append(mYear).append("-")
.append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-")
.append((mDay < 10) ? "0" + mDay : mDay));
}

/**
* 日期控件的事件
*/
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;

updateDateDisplay();
}
};

/**
* 设置时间
*/
private void setTimeOfDay(){
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
updateTimeDisplay();
}

/**
* 更新时间显示
*/
private void updateTimeDisplay(){
showTime.setText(new StringBuilder().append(mHour).append(":")
.append((mMinute < 10) ? "0" + mMinute : mMinute));
}

/**
* 时间控件事件
*/
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;

updateTimeDisplay();
}
};

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
}

return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
case TIME_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break;
}
}

/**
* 处理日期和时间控件的Handler
*/
Handler dateandtimeHandler = new Handler() {

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MainActivity.SHOW_DATAPICK:
showDialog(DATE_DIALOG_ID);
break;
case MainActivity.SHOW_TIMEPICK:
showDialog(TIME_DIALOG_ID);
break;
}
}

};

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式