我的应用程序中有两个SeekBar。一个显示小时,另一个显示间隔为15分钟(0,15,30,45)。
我的初始值设置为10.30。我在TextView中显示更改的时间。
现在,如果用户仅增加分钟搜索栏(例如15分钟),则textView应显示10.45。 (即10.30 + 00:15)。同样,如果用户进一步将分钟搜索栏增加到30或45,则TextView应分别显示11:00和11:15。
目前,一切正常,直到分钟达到15分钟。但是当我将SeekBar增加到30或45时,TextView会显示10:60和10:75。
这是我的代码: -
int hourLimit=10, minLimit=30;
seekBarHour.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar)
{
if(progressChangedHours == 0 && progressChangedMins==0)
{
txtReachingBy.setText("Reaching by:"+" "+String.valueOf(hourLimit)+":"+String.valueOf(minLimit)+":"+today.second);
}
if(progressChangedHours==0 && mins >=60)
{
hour = 1;
}
else
{
hour = hourLimit + progressChangedHours;
}
timeString = String.valueOf(hour)+":"+String.valueOf(mins)+":"+today.second;
txtReachingBy.setText("Reaching by:"+" "+timeString);
}
});
seekBarMins.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progressChangedMins = progress;
int stepSize = 15;
progressChangedMins = (progressChangedMins/stepSize)*stepSize;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar)
{
if(progressChangedMins == 0)
{
txtReachingBy.setText("Reaching by:"+" "+String.valueOf(hourLimit)+":"+String.valueOf(30)+":"+today.second);
}
else
{
mins = minLimit +progressChangedMins;
if(progressChangedHours==0 && mins!=0)
{
hour = hourLimit;
}
else if(progressChangedHours>=0 && mins>=60)
{
hour = hour+1;
mins = mins-60;
}
timeString = String.valueOf(hour)+":"+String.valueOf(mins)+":"+today.second;
txtReachingBy.setText("Reaching by:"+" "+timeString);
}
}
});
修改
:我使用Date来解决我的问题的一部分: -
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();
}
seekBarMins.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progressChangedMins = progress;
int stepSize = 15;
progressChangedMins = (progressChangedMins/stepSize)*stepSize;
seekBar.setProgress(progressChangedMins);
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(progressChangedMins==0)
{
timeString = cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+today.second;
}
else
{
cal.add(Calendar.MINUTE, progressChangedMins);
timeString = cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+today.second;
}
txtReachingBy.setText("Reaching by:"+" "+timeString);
}
});
如果我将搜索栏拇指一次拖动到一个值,现在一切正常。进度值将添加到初始时间。但是如果我将它拖动到一个值,初始时间随着加法而改变,那么如果我再拖动它,则进度值将被添加到新时间而不是初始值。
给我输出为: -
Drag 1-> 15 mins > Time expected 10.45 (Fine)
Drag 2-> 30 mins > Time expected 11.00 Output I get -> 10.45+00.30 = 11.15
我该如何解决这个问题?