Android TimePicker 问题
Android TimePicker issues
在 Android 我设置了一个带有 TimePicker 对话框的 EditText。默认时间是现在,但是使用TimePicker时间应该改变。
我有三个问题:
1)选择其他时间,有时在EditText上显示时间不变;
2) 如果我在TimePicker上提示,同时选择时间,它会变化+12h(例如现在是09.35,但显示为21.35);
3) 如果我选择一个小时 >= 17.00 我得到一个例外:
07-19 11:47:17.574 16224-16224/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 16224
java.lang.ArrayIndexOutOfBoundsException: length=17; index=18
at java.util.Calendar.set(Calendar.java:1122)
at com.example.myapp.activities.MyActivity.onTimeSet(MyActivity.java:513)
at android.app.TimePickerDialog.onClick(TimePickerDialog.java:133)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5376)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
这是我的 TimePicker 函数:
// TimePicker settings
public void setTimeFields(final EditText editText) {
// Calendar instance
final Calendar calendar = Calendar.getInstance();
// TimePicker definition
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(hourOfDay, minute);
editText.setText(new SimpleDateFormat("HH:mm", Locale.ITALIAN).format(calendar.getTime()));
}
}, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), DateFormat.is24HourFormat(this));
// Show dialog
timePickerDialog.show();
}
这是我的编辑文本定义:
etxTime = (EditText) findViewById(R.id.etxTime);
etxTime.setInputType(InputType.TYPE_NULL);
etxTime.setText(new SimpleDateFormat("HH:mm", Locale.ITALIAN).format(Calendar.getInstance().getTime()));
etxTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setTimeFields(etxTime); } });
我哪里错了?
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Show Date dialog
showDialog(Date_id);
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Show time dialog
showDialog(Time_id);
}
});
}
protected Dialog onCreateDialog(int id) {
// Get the calander
Calendar c = Calendar.getInstance();
// From calander get the year, month, day, hour, minute
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
switch (id) {
case Date_id:
// Open the datepicker dialog
return new DatePickerDialog(MainActivity.this, date_listener, year,
month, day);
case Time_id:
// Open the timepicker dialog
return new TimePickerDialog(MainActivity.this, time_listener, hour,
minute, false);
}
return null;
}
// Date picker dialog
DatePickerDialog.OnDateSetListener date_listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// store the data in one string and set it to text
String date1 = String.valueOf(month) + "/" + String.valueOf(day)
+ "/" + String.valueOf(year);
set_date.setText(date1);
}
};
TimePickerDialog.OnTimeSetListener time_listener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
// store the data in one string and set it to text
String time1 = String.valueOf(hour) + ":" + String.valueOf(minute);
set_time.setText(time1);
}
};
where "date" , "time" are buttons and "set_date", "set_time" are editTexts in this case.
出现您描述的第三个问题是因为参数 Calendar.set(int, int) are not the hour and minute, but the field 和要设置的值。如果你检查 link 中的 table 你会看到这个字段的最大 int 值是 17。所以你的代码应该如下:
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
editText.setText(new SimpleDateFormat("HH:mm", Locale.ITALIAN).format(calendar.getTime()));
}
在 Android 我设置了一个带有 TimePicker 对话框的 EditText。默认时间是现在,但是使用TimePicker时间应该改变。
我有三个问题:
1)选择其他时间,有时在EditText上显示时间不变;
2) 如果我在TimePicker上提示,同时选择时间,它会变化+12h(例如现在是09.35,但显示为21.35);
3) 如果我选择一个小时 >= 17.00 我得到一个例外:
07-19 11:47:17.574 16224-16224/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 16224
java.lang.ArrayIndexOutOfBoundsException: length=17; index=18
at java.util.Calendar.set(Calendar.java:1122)
at com.example.myapp.activities.MyActivity.onTimeSet(MyActivity.java:513)
at android.app.TimePickerDialog.onClick(TimePickerDialog.java:133)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5376)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
这是我的 TimePicker 函数:
// TimePicker settings
public void setTimeFields(final EditText editText) {
// Calendar instance
final Calendar calendar = Calendar.getInstance();
// TimePicker definition
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(hourOfDay, minute);
editText.setText(new SimpleDateFormat("HH:mm", Locale.ITALIAN).format(calendar.getTime()));
}
}, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), DateFormat.is24HourFormat(this));
// Show dialog
timePickerDialog.show();
}
这是我的编辑文本定义:
etxTime = (EditText) findViewById(R.id.etxTime);
etxTime.setInputType(InputType.TYPE_NULL);
etxTime.setText(new SimpleDateFormat("HH:mm", Locale.ITALIAN).format(Calendar.getInstance().getTime()));
etxTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setTimeFields(etxTime); } });
我哪里错了?
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Show Date dialog
showDialog(Date_id);
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Show time dialog
showDialog(Time_id);
}
});
}
protected Dialog onCreateDialog(int id) {
// Get the calander
Calendar c = Calendar.getInstance();
// From calander get the year, month, day, hour, minute
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
switch (id) {
case Date_id:
// Open the datepicker dialog
return new DatePickerDialog(MainActivity.this, date_listener, year,
month, day);
case Time_id:
// Open the timepicker dialog
return new TimePickerDialog(MainActivity.this, time_listener, hour,
minute, false);
}
return null;
}
// Date picker dialog
DatePickerDialog.OnDateSetListener date_listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// store the data in one string and set it to text
String date1 = String.valueOf(month) + "/" + String.valueOf(day)
+ "/" + String.valueOf(year);
set_date.setText(date1);
}
};
TimePickerDialog.OnTimeSetListener time_listener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
// store the data in one string and set it to text
String time1 = String.valueOf(hour) + ":" + String.valueOf(minute);
set_time.setText(time1);
}
};
where "date" , "time" are buttons and "set_date", "set_time" are editTexts in this case.
出现您描述的第三个问题是因为参数 Calendar.set(int, int) are not the hour and minute, but the field 和要设置的值。如果你检查 link 中的 table 你会看到这个字段的最大 int 值是 17。所以你的代码应该如下:
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
editText.setText(new SimpleDateFormat("HH:mm", Locale.ITALIAN).format(calendar.getTime()));
}