gpt4 book ai didi

java - HH : MM in Range Bar

转载 作者:行者123 更新时间:2023-12-01 09:57:34 24 4
gpt4 key购买 nike

我使用Ermodo Range Bar ,以及我需要以小时和分钟为单位显示最小值和最大值的事实......如下:

enter image description here

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);
}
});
}
}

现在看起来像这样:

enter image description here

我认为日期或日历类可能有帮助,但我如何使用它们?

我需要将其显示为 HH:MM 而不是 H:M

最佳答案

使用DecimalFormat就可以实现。

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

使用日历

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

调用下面的方法

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());
}

关于java - HH : MM in Range Bar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37064925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com