所以我的应用程序有2个按钮。一个打开一个datepicker对话框片段,一个打开一个timepicker片段。按钮文本显示所选的日期和时间。日期选择器工作得很好,但是我遇到了时间选择器的几个问题,这可能都是相关的。 问题1:选择器设置为12小时时间,单击AM或PM以在它们之间切换会导致应用程序崩溃(logcat将粘贴到下面)。 问题2:选择新时间后,如果再次单击该按钮,时间戳有时会显示当前时间而不是上次选择的时间。有时它完全显示一个完全不同的小时(例如,我在11.00点击它,然后将其更改为10.00。当我再次单击该按钮时,时间戳设置为下午6点)。我还没有找到重复这种行为的方法。 问题3:即使时间正确(小时和分钟)正确,有时AM / PM按钮的选择错误。
logcat的
07-29 23:59:03.483: E/Trace(2167): error opening trace file: No such file or directory (2)
07-29 23:59:12.703: E/AndroidRuntime(2167): FATAL EXCEPTION: main
07-29 23:59:12.703: E/AndroidRuntime(2167): java.lang.ArrayIndexOutOfBoundsException: length=17; index=23
07-29 23:59:12.703: E/AndroidRuntime(2167): at java.util.Calendar.set(Calendar.java:1210)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.rone.datetimepractice.PickerActivity$2.updateChangedTime(PickerActivity.java:71)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.rone.datetimepractice.TimeDialogFragment$1.onTimeSet(TimeDialogFragment.java:46)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:130)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.app.TimePickerDialog.onClick(TimePickerDialog.java:115)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.os.Looper.loop(Looper.java:137)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-29 23:59:12.703: E/AndroidRuntime(2167): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 23:59:12.703: E/AndroidRuntime(2167): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-29 23:59:12.703: E/AndroidRuntime(2167): at dalvik.system.NativeStart.main(Native Method)
来自PickerActivity的第71行的代码 - 确切的行是“now.set(hourOfDay,minute);”
public void showDialogTime() {
FragmentTransaction ftTime = getFragmentManager().beginTransaction();
fragTime = TimeDialogFragment.newInstance(this, new TimeDialogFragmentListener() {
public void updateChangedTime(int hourOfDay, int minute) {
buttonTime.setText(String.valueOf(hourOfDay)+":"+String.valueOf(minute));
now.set(hourOfDay, minute);
}}, now);
fragTime.show(ftTime, "TimeDialogFragment");
}
来自TimeDialogFragment的第46行的代码 - 特别是mListener行
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
mListener.updateChangedTime(hourOfDay, minute);
}
以下是完整的PickerActivity.java文件减去datepicker部分:
public class PickerActivity extends Activity {
TimeDialogFragment fragTime;
Button buttonDate, buttonTime;
Calendar now;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
now = Calendar.getInstance();
buttonTime = (Button) findViewById(R.id.time_button);
buttonTime.setText(String.valueOf(now.get(Calendar.HOUR))+":"+
String.valueOf(now.get(Calendar.MINUTE)));
}
public void clickMe(View v) {
int id = v.getId();
switch (id) {
case R.id.date_button :
showDialogDate();
break;
case R.id.time_button :
showDialogTime();
break;
default:
break;
}}
public interface TimeDialogFragmentListener {
public void updateChangedTime(int hourOfDay, int minute);
}
public void showDialogTime() {
FragmentTransaction ftTime = getFragmentManager().beginTransaction();
fragTime = TimeDialogFragment.newInstance(this, new TimeDialogFragmentListener() {
public void updateChangedTime(int hourOfDay, int minute) {
buttonTime.setText(String.valueOf(hourOfDay)+":"+String.valueOf(minute));
now.set(hourOfDay, minute);
}}, now);
fragTime.show(ftTime, "TimeDialogFragment");
}}
以下是TimeDialogFragment.java文件:
public class TimeDialogFragment extends DialogFragment {
static Context mContext;
static int mHour;
static int mMinute;
static TimeDialogFragmentListener mListener;
public static TimeDialogFragment newInstance(Context context,
TimeDialogFragmentListener listener, Calendar now) {
TimeDialogFragment dialog = new TimeDialogFragment();
mContext = context;
mListener = listener;
mHour = now.get(Calendar.HOUR);
mMinute = now.get(Calendar.MINUTE);
Bundle args = new Bundle();
args.putString("title", "Set Time");
dialog.setArguments(args);
return dialog;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new TimePickerDialog(mContext, mTimeSetListener, mHour, mMinute, false);
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
mListener.updateChangedTime(hourOfDay, minute);
}};}
如果我们解决这个问题有什么不同,我打算将时间保存到我的数据库中,这样我就可以按时间对结果进行排序,并允许用户将任何数据库行/记录的时间更改为他们选择的时间。我还想在按钮上包含AM / PM文本,但是我很乐意在上述问题得到解决后自己解决这些问题。任何帮助表示赞赏。
除了上述内容,如果我改变
return new TimePickerDialog(mContext, mTimeSetListener, mHour, mMinute, false);
到
return new TimePickerDialog(mContext, mTimeSetListener, mHour, mMinute, true);
在TimeDialogFragment.java文件中,当在相反的AM / PM括号中选择一个小时时,它会立即崩溃,例如。现在是0.50(12.50am),但是如果我选择23.50(晚上11.50)它会崩溃并出现与上面相同的logcat错误。更改为另一个AM时间不会导致任何问题。
答案 0 :(得分:1)
更新日历的时间时,应如下所示:
public void updateChangedTime(int hourOfDay, int minute) {
buttonTime.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
now.set(Calendar.HOUR_OF_DAY, hourOfDay);
now.set(Calendar.MINUTE, minute);
}