我有以下来自Android开发网站的代码。时间戳初始化为当前时间,这很好。当我选择一个新的时间时,小时是正确的,但分钟总是返回当前时间的分钟。
例如,如果现在是15:30,我选择16:45,那么选择器将返回16:30。 我在onTimeSet中放置了日志语句,记录的分钟是我选择的。当我将这些传递给DateTime构造函数时,不知何故,分钟是当前时间的分钟。
有谁知道这里发生了什么?
感谢
MvxMainViewModel
public static class TimePickerFromFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 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 and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
Log.e(TAG, "time picked = hourOfDay = " + hourOfDay + " minute = " + minute);
hourFrom = hourOfDay;
minuteFrom = minute;
Log.e(TAG, "minuteFrom in onTimeSet = " + minuteFrom);
DateTime dtDate = new DateTime(yearFrom, monthFrom + 1, dayFrom, hourFrom, minuteFrom);
DateTimeFormatter df = DateTimeFormat.forPattern("dd-MMM-YYYY HH:MM");
String date = df.print(dtDate);
Log.e(TAG, "formatted From date = " + date);
}
}
public void showTimePickerFromDialog(View v) {
DialogFragment newFragment = new TimePickerFromFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "timePicker");
}
答案 0 :(得分:0)
public void StartTimeShowDialogTime()
{
final StringBuilder sb_time = new StringBuilder();
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
View view1 = View.inflate(this, R.layout.dialog_time_picker, null);
mBuilder.setCancelable(false);
mBuilder.setView(view1);
Button btn_cancel = view1.findViewById(R.id.btn_cancel);
Button btn_okey = view1.findViewById(R.id.btn_okay);
final TimePicker timepicker = view1.findViewById(R.id.tp_timepicker);
Calendar currentTime = Calendar.getInstance();
int_hour = currentTime.get(Calendar.HOUR_OF_DAY);
int_minute = currentTime.get(Calendar.MINUTE);
timepicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
int_hour = hourOfDay;
int_minute = minute;
if(int_hour<10) {
int_hour = Integer.parseInt("0" + int_hour);
// Toast.makeText(Reschedule.this,
""+int_hour, Toast.LENGTH_SHORT).show();
}
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
btn_okey.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
str_tempstarttime=String.valueOf(int_hour+":"+int_minute);
str_24HoursStartTime=str_tempstarttime;
Log.e("TimeS>>",""+str_tempstarttime);
/*
to convert 24 hrs to 12 hrs with a,p
*/
try
{
if (int_hour >= 12)
{
if (int_hour == 12) {
sb_time.append(12).append(":").append(String.format("%02d", int_minute)).append("p");
} else {
sb_time.append(int_hour - 12).append(":").append(String.format("%02d", int_minute)).append("p");
}
}
else
{
if (int_hour == 12) {
sb_time.append(12).append(":").append(String.format("%02d", int_minute)).append("a");
} else{
sb_time.append(int_hour).append(":").append(String.format("%02d", int_minute)).append("a");
}
}
/*HERE IS YOUR SELECTED TIME*/
Log.e("Print",""+sb_time);
}
catch (Exception e)
{
e.printStackTrace();
}
alertDialog.dismiss();
}
});
alertDialog = mBuilder.create();
alertDialog.show();
}
答案 1 :(得分:0)
添加你的逻辑
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(CheckAvailabilityActivity.this, R.style.TimePickerTheme, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
setTime(selectedHour + ":" + selectedMinute);
}
}, hour, minute, false);//Yes 24 hour time
mTimePicker.show();