Answers:
Android没有内置提供此功能的工具。
编辑:Andriod现在提供了内置选择器。检查@Oded答案
showDialog
。也许很快会有新的事物出现。
将DatePicker
和都放置TimePicker
在布局XML中。
date_time_picker.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="true"
android:spinnersShown="false"
android:layout_weight="4"
android:layout_height="0dp" />
<TimePicker
android:id="@+id/time_picker"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp" />
<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="Set"
android:layout_height="0dp" />
</LinearLayout>
代码:
final View dialogView = View.inflate(activity, R.layout.date_time_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
time = calendar.getTimeInMillis();
alertDialog.dismiss();
}});
alertDialog.setView(dialogView);
alertDialog.show();
使用此功能,您可以一张一张地显示日期和时间,然后将其设置为全局变量日期。没有库,没有XML。
Calendar date;
public void showDateTimePicker() {
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
Log.v(TAG, "The choosen one " + date.getTime());
}
}, currentDate.get(Calendar.HOUR_OF_DAY), currentDate.get(Calendar.MINUTE), false).show();
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
}
我创建了一个图书馆来做到这一点。它还具有可自定义的颜色!
使用非常简单。
首先,您创建一个侦听器:
private SlideDateTimeListener listener = new SlideDateTimeListener() {
@Override
public void onDateTimeSet(Date date)
{
// Do something with the date. This Date object contains
// the date and time that the user has selected.
}
@Override
public void onDateTimeCancel()
{
// Overriding onDateTimeCancel() is optional.
}
};
然后创建并显示对话框:
new SlideDateTimePicker.Builder(getSupportFragmentManager())
.setListener(listener)
.setInitialDate(new Date())
.build()
.show();
希望对你有帮助。
在DatePicker
更新时间方法中调用时间选择器对话框。当您按下DatePicker
设置按钮时,不会同时调用它。时间选择器对话框将打开。该方法在下面给出。
package com.android.date;
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.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class datepicker extends Activity {
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
private TextView mTimeDisplay;
private Button mPickTime;
private int mhour;
private int mminute;
static final int TIME_DIALOG_ID = 1;
static final int DATE_DIALOG_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDateDisplay =(TextView)findViewById(R.id.date);
mPickDate =(Button)findViewById(R.id.datepicker);
mTimeDisplay = (TextView) findViewById(R.id.time);
mPickTime = (Button) findViewById(R.id.timepicker);
//Pick time's click event listener
mPickTime.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
//PickDate's click event listener
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
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);
}
//-------------------------------------------update date---//
private void updateDate() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("/")
.append(mMonth + 1).append("/")
.append(mYear).append(" "));
showDialog(TIME_DIALOG_ID);
}
//-------------------------------------------update time---//
public void updatetime() {
mTimeDisplay.setText(
new StringBuilder()
.append(pad(mhour)).append(":")
.append(pad(mminute)));
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
//Datepicker dialog generation
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDate();
}
};
// Timepicker dialog generation
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mhour = hourOfDay;
mminute = minute;
updatetime();
}
};
@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, false);
}
return null;
}
}
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<TextView android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<Button
android:id="@+id/timepicker"
android:text="Change Time"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<Button android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="200dp"
android:text="Change the date"/>
</LinearLayout>
我还想结合使用DatePicker和TimePicker。因此,我创建了一个API来在一个接口中处理两者!:)
https://github.com/Kunzisoft/Android-SwitchDateTimePicker
您也可以使用SublimePicker
<item name="android:textSize">11sp</item>
并删除粗体设置。2)看起来像setDefaultDateTime(..)
必须被调用,否则您将获得运行时异常..所以我将其Calendar.getInstance().getTime()
作为参数调用。到目前为止,我看到的最好的日期选择器github项目-感谢您的分享
您所链接的URL,显示了如何弹出一个带有时间选择器的对话框以及另一个带有日期选择器的对话框。但是,您需要注意,可以直接在自己的布局中使用这些UI小部件。您可以在同一视图中构建自己的对话框,其中同时包含TimePicker和DatePicker,从而完成我认为要查找的内容。
换句话说,不使用TimePickerDialog和DatePickerDialog,而是直接在您自己的对话框或活动中使用UI小部件TimePicker和DatePicker。
我在一个项目中遇到了同样的问题,因此决定制作一个自定义窗口小部件,该窗口小部件在一个用户友好的对话框中同时包含日期和时间选择器。您可以在http://code.google.com/p/datetimepicker/上获取源代码和示例。该代码已在Apache 2.0下获得许可。
您可以使用DatePicker library
wdullaer / MaterialDateTimePicker之一
首先显示DatePicker。
private void showDatePicker() {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
HomeActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.show(getFragmentManager(), "Choose Date:");
}
然后 onDateSet callback store date & show TimePicker
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
Calendar cal = Calendar.getInstance();
cal.set(year, monthOfYear, dayOfMonth);
filter.setDate(cal.getTime());
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
showTimePicker();
}
},500);
}
在onTimeSet callback
店内的时间
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
Calendar cal = Calendar.getInstance();
if(filter.getDate()!=null)
cal.setTime(filter.getDate());
cal.set(Calendar.HOUR_OF_DAY,hourOfDay);
cal.set(Calendar.MINUTE,minute);
}
另一个选择是android-wheel项目,它非常接近iOS的UIDatePicker对话框。
它提供了一个垂直滑块来选择任何内容(包括日期和时间)。如果您更喜欢水平滑块,则Rabi引用的DateSlider更好。
我创建了一个警报对话框,其中将日期选择器和时间选择器结合在一起。您可以在https://github.com/nguyentoantuit/android上获取代码 注意:DateTimePicker是一个库
这是Jaydeep的一个更紧凑的版本,它显示一个对话框,另一个对话框。我喜欢这种解决方案,因为它没有依赖性。
Date value = new Date();
final Calendar cal = Calendar.getInstance();
cal.setTime(value);
new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override public void onDateSet(DatePicker view,
int y, int m, int d) {
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m);
cal.set(Calendar.DAY_OF_MONTH, d);
// now show the time picker
new TimePickerDialog(NoteEditor.this,
new TimePickerDialog.OnTimeSetListener() {
@Override public void onTimeSet(TimePicker view,
int h, int min) {
cal.set(Calendar.HOUR_OF_DAY, h);
cal.set(Calendar.MINUTE, min);
value = cal.getTime();
}
}, cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE), true).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)).show();