在onTimeSet方法中的赋值语句会被执行两次,后发现这个方法会被执行那个两次
monthEnd = monthEnd+1;
Log.e("month==value", "month:"+monthEnd);
本来系统的月份比实际少一个月,加1就可以了,点一次执行两次就相当于,我选的7月,而实际却变成了9月。
onDateSet方法把month的值赋值给了成员monthMember即monthMember = month,在onTimeSet中把month+1设置给了textview,即textview.setText(month+1),这个时候就会自动加2,所以避免这种执行两次的情况。
网上一搜,说这是android4.1和4.2存在的一个bug,点击确定和取消按钮时,会出发onTimeSet;在dialog的onStop(比如dialog dismiss时)中,也调用了onTimeSet方法。需要修改framework里的TimePickerDialog.java源文件
所以说复写对话框注掉onStop就行了,
@Override
protected void onStop() {
// TODO Auto-generated method stub
//super.onStop();
}
protected void onStop() {
// TODO Auto-generated method stub
//super.onStop();
}
我的解决办法是
1:在onDateSet方法中monthMember = month+1;来完成赋值,然后再把monthMember设置给textview,onTimeSet中只是完成了textview.settext(monthMember);并没有加1的操作,即使是onTimeSet执行两次也无所谓,不会受到影响。
endTime.setOnClickListener(new OnClickListener() { private int yearEnd = 0; private int monthEnd = 0; private int dayEnd = 0; @Override public void onClick(View v) { TimePickerDialog tpDialog = new TimePickerDialog(StartActivity.this, new OnTimeSetListener() { private String hour2_02; private String minute2_02; @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { System.out.println("--timePicker-----" + hourOfDay + "-----" + minute); String hour2_01 = hourOfDay + ""; if (hour2_01.length() == 1) { hour2_02 = "0" + hour2_01; } else { hour2_02 = hour2_01; } String minute2_01 = minute + ""; if (minute2_01.length() == 1) { minute2_02 = "0" + minute2_01; } else { minute2_02 = minute2_01; } Log.e("SSSSS", "hour2_02:"+hour2_02+",minute2_02"+minute2_02); list_time.add(hour2_02 + ":" + minute2_02 + ":00"); int hourTemp = hourOfDay - 3; if(hourTemp <=0){ hourTemp = hourTemp+12; } Log.e("设置date", "设置date"); monthEnd = monthEnd+1; Log.e("month==value", "month:"+monthEnd); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); int hourVariable = cal.get(Calendar.HOUR); int minVariable = cal.get(Calendar.MINUTE); int yearVariable = cal.get(Calendar.YEAR); int monthVariable = cal.get(Calendar.MONTH); int dayVariable = cal.get(Calendar.DAY_OF_MONTH); if(yearEnd>yearVariable){//选的时间大于当前时间 yearEnd = yearVariable; if(monthEnd>minVariable){ monthEnd = minVariable; if(dayEnd > dayVariable){ dayEnd = dayVariable; if(hourOfDay > hourVariable){ hourOfDay = hourVariable; if(minute > minVariable){ minute = minVariable; } } } } } endTime.setText(yearEnd+"-"+ monthEnd+"-"+dayEnd+" "+hourOfDay+":"+minute+":00"); startTime.setText(yearEnd+"-"+ monthEnd+"-"+dayEnd+" "+hourTemp+":"+minute+":00");//这个是显示最新 的 } }, hourVariable, minVariable, false); tpDialog.show(); DatePickerDialog dpDialog = new DatePickerDialog(StartActivity.this,new OnDateSetListener() { private String month2_02; private String day2_02; private String hour2_02; private String minute2_02; public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { if (list_time.size() == 0) { // 时间没选 int i = Calendar.HOUR_OF_DAY; int minute = Calendar.MINUTE; String hour2_01 = i + ""; if (hour2_01.length() == 1) { hour2_02 = "0" + hour2_01; } else { hour2_02 = hour2_01; } String minute2_01 = minute + ""; if (minute2_01.length() == 1) { minute2_02 = "0" + minute2_01; } else { minute2_02 = minute2_01; } list_time.add(hour2_02 + ":" + minute2_02 + ":00"); Log.e("SSSSS2", "hour2_02:"+hour2_02+",minute2_02"+minute2_02); } System.out.println("--gjhf------datePicker---------" + year + "---" + monthOfYear + "----" +dayOfMonth + "---"); System.out.println("---end time------" + list_time.get(list_time.size() - 1)); String month2_01 = (monthOfYear + 1) + ""; if (month2_01.length() == 1) { month2_02 = "0" + month2_01; } else { month2_02 = month2_01; } String day2_01 = dayOfMonth + ""; if (day2_01.length() == 1) { day2_02 = "0" + day2_01; } else { day2_02 = day2_01; } list_data_time.add(year + "-" + month2_02 + "-" + day2_02 + list_time.get(list_time.size() - 1)); // dialog.cancel(); /*if (flag_dialog) { dialogData(); flag_dialog = false; }*/ Log.e("==========", "year"+year); // startTime.setText(year+"-"+ monthOfYear+"-"+dayOfMonth+" "+hour2_02+":"+minute2_02); // startTime.setText(list_data_time.get(list_time.size() - 1));//这里不能设置,顶多能保存 Log.e("date -time11 ", list_data_time.get(list_time.size() - 1)); yearEnd = year; monthEnd = monthOfYear; dayEnd = dayOfMonth; } }, yearVariable, monthVariable-1, dayVariable); dpDialog.show(); } });
2:第二种解决办法:
//复写对话框注掉onStop就行了, @Override protected void onStop() { // TODO Auto-generated method stub //super.onStop(); }