我正在尝试创建一个在同一个 Activity 中同时使用 Datepickers 和 Timepickers 对话框的应用程序。我使用这些链接来帮助我弄清楚如何实现日期选择器和时间选择器: http : //www.mkyong.com/android/android-time-picker-example/ http://www.mkyong.com/android/android -日期选择器示例/
这两个链接的设计非常相似。但是,当我尝试将它们一起使用时,只有一个对话框弹出,而另一个没有。Timepicker 工作正常,它是第二个对话框。这是我的代码:
package com.example.meetingplanner;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class MakeMeetingActivity extends Activity {
//_____________________________
public TextView CurrentDate;
public TextView LabelDateTest;
public Button Datebutton;
public int year;
public int month;
public int day;
static final int DATE_DIALOG_ID = 999;
//_____________________________
public TextView CurrentTime;
public TextView LabelTimeTest;
public Button TimeButton;
public int hour;
public int minute;
static final int TIME_DIALOG_ID = 999;
//_____________________________
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_makemeeting);
setCurrentDateOnView();
addListenerOnButtonDATE();
setCurrentTimeOnView();
addListenerOnButtonTIME();
}
public void setCurrentDateOnView() {
CurrentDate = (TextView) findViewById(R.id.CurrentDate);
LabelDateTest = (TextView) findViewById(R.id.DateTest);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
CurrentDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
}
public void addListenerOnButtonDATE() {
Datebutton = (Button) findViewById(R.id.DateButton);
Datebutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
protected Dialog onCreateDialogDATE(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
public DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when data is entered, everything will be appended
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
CurrentDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
LabelDateTest.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
// display current time
public void setCurrentTimeOnView() {
CurrentTime = (TextView) findViewById(R.id.CurrentTime);
LabelTimeTest = (TextView) findViewById(R.id.CurrentTime);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into textview
CurrentTime.setText(
new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
}
public void addListenerOnButtonTIME() {
TimeButton = (Button) findViewById(R.id.TimeButton);
TimeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,false);
}
return null;
}
public TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
// set current time into textview
CurrentTime.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
LabelTimeTest.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
}
};
public static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
这就是我的代码,时间选择器可以工作,但日期选择器什么也不做。一个类似但问得不好的问题是:Problem with datepicker and timepicker
编辑:我看到有些人认为 theDATE_DIALOG_ID
和 theTIME_DIALOG_ID
应该有不同的值,但是当我这样做时,Datepicker 按钮仍然不起作用