2016-05-06 46 views
0

我用Ermodo Range Bar,而事實上,我需要顯示的最小值和以小時和分鐘的最大價值......這下面是:HH:MM在範圍杆

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

+0

閱讀'android.text.format.DateUtils'文檔 – pskink

回答

1

使用DecimalFormat就可以實現。

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

OR 使用日曆

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

happyCoding;

+0

這不是正確的做法,你需要使用日曆或日期類。 –

+0

@KakshilShah你有沒有看到他的代碼..他沒有使用'Calendar'或'Date'實例..主要是計算'minHours','minMiute'等... – Bharatesh

+0

是的,他應該使用兩個日期(只有時間),並使用它指向最小值和最大值。 –