0

我的初始时间值为 10:30。我也有两个 SeekBar,一个用于小时数,另一个用于分钟数。

当我将 SeekBar 滑动到某个值时,例如对于 Hour SeekBar,小时数会添加到 10:30 的初始时间。这工作正常。

此外,如果我再次将 SeekBar 拖动到一个新值,则该值应添加到初始值,而不是我在第一次拖动后获得的值。

IE。如果一开始我将 SeekBar 拖到 1 小时并离开 SeekBar,则时间为 11.30。

如果我再次将值拖到 3 小时,则 3 小时将添加到 11.30,使时间为 2:30,而我希望时间为 1:30

这是我的代码:-

String initialTime = "10:30"
cal = Calendar.getInstance();


        DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        Date date;
        try 
        {
            date = sdf.parse(initialTime);
            cal= Calendar.getInstance();
            cal.setTime(date);

            txtReachingBy.setText("Reaching By:"+" "+ cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
seekLateBy.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
        {


            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
            {
                progressChangedHours = progress;
                if(progressChangedHours==0 && progressChangedMins!=0)
                {
                    lblLateBy.setText("Late By (Hrs):"+" " + String.valueOf(progressChangedHours) +":"+String.valueOf(progressChangedMins));
                }
                else
                lblLateBy.setText("Late By (Hrs):"+" " + String.valueOf(progressChangedHours) +":"+String.valueOf(progressChangedMins));

            }



            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) 
            {
                if(progressChangedHours==0)
                {
                    timeString = cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+today.second;
                }
                else
                {
                cal.add(Calendar.HOUR_OF_DAY, progressChangedHours);

                timeString = cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+today.second;
                }
                txtReachingBy.setText("Reaching by:"+" "+timeString);
            }





        }); 

我怎样才能做到这一点/

4

1 回答 1

0

根据您的要求,您需要另一个变量来存储进度更改的先前值。

检查下面的代码:

    int previousval=0;
    seekLateBy.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    {
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {

            progressChangedHours = progress;
       // DO this code if previousval==0
          if(previousval==0){
             previousval = progress;
            if(progressChangedHours==0 && progressChangedMins!=0)
            {
                lblLateBy.setText("Late By (Hrs):"+" " +   String.valueOf(progressChangedHours) +":"+String.valueOf(progressChangedMins));
            }
            else
            lblLateBy.setText("Late By (Hrs):"+" " + String.valueOf(progressChangedHours) +":"+String.valueOf(progressChangedMins));

        }
     }else{
    // Value is already there.add with the new value
            if(progressChangedHours==0 && progressChangedMins!=0)
            {
                lblLateBy.setText("Late By (Hrs):"+" " +   String.valueOf(progressChangedHours+previousval) +":"+String.valueOf(progressChangedMins));
            }
            else
            lblLateBy.setText("Late By (Hrs):"+" " + String.valueOf(progressChangedHours+previousval) +":"+String.valueOf(progressChangedMins));

        }
    }
于 2014-09-17T10:27:16.717 回答