我已经有一个DatePicker,当用户点击EditText字段时会弹出
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字段后,我甚至尝试弹出TimePicker。
eReminderTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(TIME_DIALOG_ID);
}
});
出于某种原因,当我进入Android Studio时,'showDialog'得分了。
任何人都可以给我一些关于我哪里出错的提示吗?或者我只是要使用自定义DialogFragment?
答案 0 :(得分:198)
eReminderTime.setText( "" + selectedHour + ":" + selectedMinute);
您在+
和选定的小时""
方法之间缺少setText
只占用一个字符串,因此您需要连接所有部分(第一个引号可能是不必要的)。
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 Constructors
答案 1 :(得分:23)
为什么不以可重复使用的方式写作?
创建SetTime类:
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);
答案 2 :(得分:8)
您可以在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();
查看完整代码
答案 3 :(得分:3)
我不知道这对你有用,对我来说很合适。
为日期/时间选择器对话框创建方法。
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();
}
希望您能将此作为您的解决方案。
答案 4 :(得分:2)
我扩展了@Sumoanand的优秀可重用解决方案,在多次更改时间时支持焦点更改和单击侦听器。同时更新选择器日历以记住上次选择的时间+格式化HH:mm
public class TimeSetter implements View.OnFocusChangeListener, TimePickerDialog.OnTimeSetListener, View.OnClickListener {
private EditText mEditText;
private Calendar mCalendar;
private SimpleDateFormat mFormat;
public TimeSetter(EditText editText){
this.mEditText = editText;
mEditText.setOnFocusChangeListener(this);
mEditText.setOnClickListener(this);
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
showPicker(view);
}
}
@Override
public void onClick(View view) {
showPicker(view);
}
private void showPicker(View view) {
if (mCalendar == null)
mCalendar = Calendar.getInstance();
int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
int minute = mCalendar.get(Calendar.MINUTE);
new TimePickerDialog(view.getContext(), this, hour, minute, true).show();
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
if (mFormat == null)
mFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
this.mEditText.setText(mFormat.format(mCalendar.getTime()));
}
}
onCreate的用法:
EditText timeEditText = (EditText) rootView.findViewById(R.id.timeText);
new TimeSetter(timeEditText);
答案 5 :(得分:1)
您尚未将最后一个参数放在TimePickerDialog
。
{
public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,
boolean is24HourView) {
this(context, 0, listener, hourOfDay, minute, is24HourView);
}
}
这是TimePickerclass
的代码。它需要一个布尔参数is24HourView
答案 6 :(得分:0)
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);
}
答案 7 :(得分:0)
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();
}
}
答案 8 :(得分:0)
对于我来说,如果我多次单击dpFlightDate编辑文本(对于调光器对话框),则对话框显示多个对话框。 如何避免该对话框仅出现一次,并且如果用户单击第二次,则该对话框一定不能再次出现,即对话框是否出现在屏幕上?
// perform click event on edit text
dpFlightDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(frmFlightDetails.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
dpFlightDate.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
tpFlightTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog
timePickerDialog = new TimePickerDialog(frmFlightDetails.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
tpFlightTime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
});
答案 9 :(得分:0)
在上述所有情况下,EditText 仍然需要 xml 中的 focusable="false"
属性,以防止键盘弹出。