问题
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.
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
What I did is here.
Activity Class
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));
}
});
here
回答1:
set setNotifyWhileDragging option
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);
rangebar.setNotifyWhileDragging(true);
And set onRangeSeekBarValuesChanged Listener
rangebar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
@Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
// set bar value And Text
}
});
回答2:
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);
And I believe that you should specify the max range as rangebar.setTickCount(24 * SMALLEST_HOUR_FRACTION); (there are 24 hours per day).
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
来源:https://stackoverflow.com/questions/46261792/set-time-in-hhmm-format-in-seek-range-bar