0

我的应用程序中有两个 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);
            }
        }



    }); 

编辑

:我使用日期来解决我的部分问题,如下所示:-

    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

我该如何解决这个问题?

4

0 回答 0