如何更改同一活动中第二个timepicker对话框中的时间?

时间:2012-10-26 10:40:10

标签: android timepicker

你好我在同一个活动中使用两个时间选择器。我的要求是第一次timepicker将显示当前时间,第二个将显示(当前小时+1)但我的问题是第二个没有显示(当前小时+1) )它还显示当前时间。这是我的代码

  if(v.equals(stimeimage))
  {
   stime=true;
   myYear = c.get(Calendar.YEAR);
   myMonth = c.get(Calendar.MONTH);
   myDay = c.get(Calendar.DAY_OF_MONTH);
   showDialog(DATE_DIALOG_ID); 
  }
  if(v.equals(etimeimage))
  {   
   etime=true;
   myYear = c.get(Calendar.YEAR);
   myMonth = c.get(Calendar.MONTH);
   myDay = c.get(Calendar.DAY_OF_MONTH);
   showDialog(DATE_DIALOG_ID);       
  }
  private DatePickerDialog.OnDateSetListener myDateSetListener
                     = new DatePickerDialog.OnDateSetListener(){

 public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) 
 {
  if(monthOfYear+1<10)
   datetext = String.valueOf(year) + "-0"+ String.valueOf(monthOfYear+1) + "-"+ String.valueOf(dayOfMonth);
  else
   datetext = String.valueOf(year) + "-"+ String.valueOf(monthOfYear+1) + "-"+ String.valueOf(dayOfMonth); 
  Log.e("datetext",datetext);

   myHour = c.get(Calendar.HOUR_OF_DAY);
   if(etime)
    myHour = c.get(Calendar.HOUR_OF_DAY)+1; 
   Log.e("mhour",""+myHour);
   myMinute = c.get(Calendar.MINUTE);
   showDialog(TIME_DIALOG_ID);    
   }
  };

  protected Dialog onCreateDialog(int id) 
  {
      AlertDialog myDialog = null;
   switch(id){
    case DATE_DIALOG_ID:{
    myDialog= new DatePickerDialog(this,myDateSetListener,myYear, myMonth, myDay);
    break;
    }
    case TIME_DIALOG_ID:{
            myDialog= new TimePickerDialog(this,myTimeSetListener,myHour, myMinute, false);
            break;
    }

   }
   return myDialog;
  }

当我在日志中按下第二个时间戳时它会显示(currenthour + 1)但显示当前时间..请帮我解决问题..

我google了很多,但没有找到任何解决方案..

提前致谢

1 个答案:

答案 0 :(得分:0)

    timePicker.setCurrentHour(currentHour+1);

在oncreate方法中以编程方式添加它