0
我想將EditText字符串中的小時和分鐘傳遞給日曆對象,然後將小時和分鐘放入Bundle中以設置爲TimePickerFragment。該字符串來自用戶在EditText行上輸入的以前選擇的時間。我使用「Integer.parseInt」將字符串分成小時和分鐘部分。當我運行時,我得到「Invalid int:」07PM「[這是從下午5:07分鐘]。其他錯誤行說:」在java.lang.Integer.invalidInt java.lang.Integer.parse在Java 。.lang.Integer.parseInt」不正常的解析,因爲‘被列入分裂PM’是什麼原因造成的問題,任何其他的想法如何在Android Bundle中傳遞小時和分鐘?
活動文件:??
{ Calendar tcal = Calendar.getInstance();
String timeStr = gEditText.getText().toString().replace(" ", "");
String[]timeParts = timeStr.split(":");
tcal.set(Calendar.HOUR, Integer.parseInt(timeParts[0]));
tcal.set(Calendar.MINUTE, Integer.parseInt(timeParts[1]));
Bundle timebundle = new Bundle();
timebundle.putInt("hour", tcal.get(Calendar.HOUR));
timebundle.putInt("minute", tcal.get(Calendar.MINUTE));
}
TimePicker文件:
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (getArguments() != null) {
cal = Calendar.getInstance();
hour = getArguments().getInt("hour");
minute = getArguments().getInt("minute");
cal.set(Calendar.HOUR, Calendar.MINUTE);
}
TimePickerDialog dialog = new TimePickerDialog(getActivity(), this , hour, minute, false);
dialog.setTitle("Select a Due Time");
return dialog;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
txtTime = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.GEditText);
String AM_PM = "";
Calendar displaytime = Calendar.getInstance();
displaytime.set(Calendar.HOUR_OF_DAY, hourOfDay);
displaytime.set(Calendar.MINUTE, minute);
if (displaytime.get(Calendar.AM_PM) == Calendar.AM)
AM_PM = "AM";
else if (displaytime.get(Calendar.AM_PM) == Calendar.PM)
AM_PM = "PM";
String minuteString;
if (minute < 10)
minuteString = "0" + minute;
else
minuteString = "" + minute;
String hours = (displaytime.get(Calendar.HOUR)== 0) ? "12" : displaytime.get(Calendar.HOUR) + "";
txtTime.setText(hours + ":" + minuteString + " " + AM_PM + " ");
txtTime.setSelection(txtTime.getText().length());
}
好吧,我想我明白了。基本上從時間的微小部分中刪除AM或PM。正確?另外,爲什麼「小時=小時+ 12」? – AJW
@AJW因爲下午5:07 = 17:07 –
好吧,我看到,如果只是下午時間。我會試一試,謝謝。 – AJW