HH: MM in Range Bar

空扰寡人 提交于 2019-12-12 03:29:42

问题


I use Ermodo Range Bar, and the fact that I need to display a minimum value and a maximum value in hours and minutes ... That is below:

public class FilterActivity extends Activity {
private RangeBar rangebar;
final int SMALLEST_HOUR_FRACTION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filter_layout);

final TextView mDepartMin = (TextView)findViewById(R.id.tvDepartMin);
final TextView mDepartMax = (TextView)findViewById(R.id.tvDepartMax);
rangebar = (RangeBar) findViewById(R.id.rangebar1);
rangebar.setTickCount(25 * SMALLEST_HOUR_FRACTION);
rangebar.setTickHeight(0);
rangebar.setThumbRadius(8);
rangebar.setConnectingLineWeight(3);

mDepartMin.setText("" + (rangebar.getLeftIndex() / SMALLEST_HOUR_FRACTION) + ":" + SMALLEST_HOUR_FRACTION * (rangebar.getLeftIndex() % SMALLEST_HOUR_FRACTION));
mDepartMax.setText("" + (rangebar.getRightIndex() / SMALLEST_HOUR_FRACTION) + ":" + SMALLEST_HOUR_FRACTION * (rangebar.getRightIndex() % SMALLEST_HOUR_FRACTION));

rangebar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
    @Override
    public void onIndexChangeListener(RangeBar rangeBar, int leftThumbIndex, int 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);
        mDepartMin.setText(minHour + ":" + minMinute);
        mDepartMax.setText(maxHour + ":" + maxMinute);
    }
});
}
}

Now it looks like this:

I think date or calendar class may help, but how do I use them?

I need it to be displayed as HH:MM instead of H:M


回答1:


Using DecimalFormat you can achieve.

DecimalFormat deciFormat= new DecimalFormat("00");
mDepartMin.setText(deciFormat.format(minHour) + ":" + deciFormat.format(minMinute));

OR Using Calendar

mDepartMin.setText(getFormattedDate(minHour,minMinute));

Call this below method

public static String getFormattedDate(int hour, int minute) {
    Calendar cale = Calendar.getInstance();
    cale.set(Calendar.HOUR_OF_DAY, hour);
    cale.set(Calendar.MINUTE, minute);
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
    return dateFormat.format(cale.getTime());
}


来源:https://stackoverflow.com/questions/37064925/hh-mm-in-range-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!