TimePicker对话框,单击EditText

我已经试过看这个问题的很多例子,并已经提出这个问题,但没有提交任何我曾经试过的代码。

我已经有了一个DatePicker,当用户点击EditText字段时popup

eReminderDate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //To show current date in the datepicker Calendar mcurrentDate = Calendar.getInstance(); int mYear = mcurrentDate.get(Calendar.YEAR); int mMonth = mcurrentDate.get(Calendar.MONTH); int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH); DatePickerDialog mDatePicker; mDatePicker = new DatePickerDialog(AddReminder.this, new OnDateSetListener() { public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) { // TODO Auto-generated method stub /* Your code to get date and time */ selectedmonth = selectedmonth + 1; eReminderDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear); } }, mYear, mMonth, mDay); mDatePicker.setTitle("Select Date"); mDatePicker.show(); } }); 

我试过用类似的方法做一个TimePicker,但是无法使它工作。 这是我试图让它工作

  eReminderTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Calendar mcurrentTime = Calendar.getInstance(); int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); int minute = mcurrentTime.get(Calendar.MINUTE); TimePickerDialog mTimePicker; mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { eReminderTime.setText( ""selectedHour + ":" + selectedMinute); } }, hour, minute); mTimePicker.setTitle("Select Time"); mTimePicker.show(); } }); 

是不可能做到这一点类似于我为DatePicker做的吗?

我已经试过,即使只是使用这个代码点击EditText字段时popup一个TimePicker。

  eReminderTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(TIME_DIALOG_ID); } }); 

出于某种原因,当我将其input到Android Studio中时,“showDialog”被logging下来。

任何人都可以给我提示我要去哪里错了吗? 或者我只是将不得不使用自定义DialogFragment?

 eReminderTime.setText( "" + selectedHour + ":" + selectedMinute); 

你在""和选定小时之间缺less一个+setText方法只需要一个string,所以你需要连接所有的部分(第一个引号可能是不必要的)。

 eReminderTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Calendar mcurrentTime = Calendar.getInstance(); int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); int minute = mcurrentTime.get(Calendar.MINUTE); TimePickerDialog mTimePicker; mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { eReminderTime.setText( selectedHour + ":" + selectedMinute); } }, hour, minute, true);//Yes 24 hour time mTimePicker.setTitle("Select Time"); mTimePicker.show(); } }); 

这应该解决你的第二个错误,你没有提供最后一个参数。 TimePickerDialog构造函数

为什么不以可重复使用的方式写入?

创buildSetTime类:

 class SetTime implements OnFocusChangeListener, OnTimeSetListener { private EditText editText; private Calendar myCalendar; public SetTime(EditText editText, Context ctx){ this.editText = editText; this.editText.setOnFocusChangeListener(this); this.myCalendar = Calendar.getInstance(); } @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if(hasFocus){ int hour = myCalendar.get(Calendar.HOUR_OF_DAY); int minute = myCalendar.get(Calendar.MINUTE); new TimePickerDialog(ctx, this, hour, minute, true).show(); } } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub this.editText.setText( hourOfDay + ":" + minute); } } 

然后从onCreate函数调用它:

  EditText editTextFromTime = (EditText) findViewById(R.id.editTextFromTime); SetTime fromTime = new SetTime(editTextFromTime, this); 

您可以在edittext的onclick监听器中使用下面的代码

  TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { tv_time.setText(hourOfDay + ":" + minute); } }, hour, minute, false); timePickerDialog.show(); 

您可以在Android timepicker示例中看到完整的代码

我不知道这是否会为你工作,这对我来说很好。

为date/时间select器对话框创build一个方法。

 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is called, below method will be called. // The arguments will be working to get the Day of Week to show it in a special TextView for it. public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { String year1 = String.valueOf(selectedYear); String month1 = String.valueOf(selectedMonth + 1); String day1 = String.valueOf(selectedDay); delivDate.setText(month1 + "/" + day1 + "/" + year1); delivDay.setText(DateFormat.format("EEEE", new Date(selectedYear, selectedMonth, selectedDay - 1)).toString()); } }; 

然后,无论你想要什么,都可以这样做

 public void setDateOnClick (View view) { Calendar cal = Calendar.getInstance(); DatePickerDialog datePicker = new DatePickerDialog(this, datePickerListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); //Create a cancel button and set the title of the dialog. datePicker.setCancelable(false); datePicker.setTitle("Select the date"); datePicker.show(); } 

希望你find这个解决scheme。

 public void onClick1(View v) { DatePickerDialog dialog = new DatePickerDialog(this, this, 2013, 2, 18); dialog.show(); } public void onDateSet1(DatePicker view, int year1, int month1, int day1) { e1.setText(day1 + "/" + (month1+1) + "/" + year1); } 

你没有把最后一个参数放在TimePickerDialog

 { public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView) { this(context, 0, listener, hourOfDay, minute, is24HourView); } } 

这是TimePickerclass的代码。 它需要一个布尔参数is24HourView

 public class **Your java Class** extends ActionBarActivity implements View.OnClickListener{ date = (EditText) findViewById(R.id.date); date.setInputType(InputType.TYPE_NULL); date.requestFocus(); date.setOnClickListener(this); dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US); setDateTimeField(); private void setDateTimeField() { Calendar newCalendar = Calendar.getInstance(); fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Calendar newDate = Calendar.getInstance(); newDate.set(year, monthOfYear, dayOfMonth); date.setText(dateFormatter.format(newDate.getTime())); } }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); } @Override public void onClick(View v) { fromDatePickerDialog.show(); } }