在搜索范围栏中以 HH:MM 格式设置时间

问题描述:

我想像这样在范围栏中显示时间.我想要像 HH:mm 这样的时间格式的最小值和最大值.我正在获得输出但并不完美.我想设置从早上 6:00 到晚上 11:59 的值.

I want to display time in range bar like this. I want to its minimum and maximum value in time format like HH:mm style. I am getting output but not in perfect. I want to set my value from morning 6:00 AM to Night 11:59 PM.

我哪里出错了?我希望我的约会间隔为 15 分钟,例如 6:00--> 6:15--> 6:30-->6:45...等等

Where I am making a mistake? I want my date in 15 minutes interval like 6:00--> 6:15--> 6:30-->6:45... and so on

我所做的就在这里.

活动类

enter code   // Gets the RangeBar
    rangebar = (RangeBar) findViewById(R.id.rangebar1);
    rangebar.setTickCount(25 * SMALLEST_HOUR_FRACTION);//SMALLEST_HOUR_FRACTION = 4;
    rangebar.setTickHeight(0);
    rangebar.setThumbRadius(8);
    rangebar.setConnectingLineWeight(3);



    // Sets the display values of the indices
    rangebar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
        @Override
        public void onIndexChangeListener(RangeBar rangeBar, int leftThumbIndex, int rightThumbIndex) {
            DecimalFormat deciFormat= new DecimalFormat("00");

            leftIndexValue.setText("" + leftThumbIndex);
            rightIndexValue.setText("" + rightThumbIndex);

            int minHour = leftThumbIndex / SMALLEST_HOUR_FRACTION;
            int minMinute = SMALLEST_HOUR_FRACTION * (leftThumbIndex % SMALLEST_HOUR_FRACTION);
            int maxHour = rightThumbIndex / SMALLEST_HOUR_FRACTION;
            int maxMinute = SMALLEST_HOUR_FRACTION * (rightThumbIndex % SMALLEST_HOUR_FRACTION);
            leftIndexValue.setText(minHour + ":" + minMinute);
            rightIndexValue.setText(maxHour + ":" + maxMinute);

            leftIndexValue.setText(deciFormat.format(minHour) + ":" + deciFormat.format(minMinute));
            rightIndexValue.setText(deciFormat.format(maxHour) + ":" + deciFormat.format(maxMinute));
        }
    });

这里

我认为根据拇指索引计算小时和分钟是错误的.有一个适当的计算:

I think there is a mistake in calculating hours and minutes based on thumb indexes. There is a proper calculation:

hours = thumb / SMALLEST_HOUR_FRACTION; // SMALLEST_HOUR_FRACTION should be defined as int
minute = (thumb % SMALLEST_HOUR_FRACTION) * (60 / (float) SMALLEST_HOUR_FRACTION);

而且我相信您应该将最大范围指定为 rangebar.setTickCount(24 * SMALLEST_HOUR_FRACTION);(一天有 24 小时).

And I believe that you should specify the max range as rangebar.setTickCount(24 * SMALLEST_HOUR_FRACTION); (there are 24 hours per day).

但是有一种更好的方式来格式化日期和时间.查看 Calendar 和 SimpleDateFormat 类:Android 将日历格式化为输出时间

But there is a better way to format dates and times. Take a look at Calendar and SimpleDateFormat classes: Android Format calendar to output time