我无法理解为什么我试图用这个for循环构建的字符串只返回一个字符。我有一个4字符的字符串,我迭代所有匹配'0'的字符,但逻辑只发生一次。我错过了什么?
private void updateDurationColor(SpinClassMovement movement){
String duration = (String) TextFormatUtil.getFormattedTimeInMinutesAndSeconds(movement.getMovementLengthInMinutes() + movement.getMovementLengthInSeconds());
for(int i = 0; i < duration.length(); i++){
if (duration.charAt(i) == '0'){
Character zero = duration.charAt(i);
StringBuilder colorDuration = new StringBuilder(zero);
colorDuration.append(zero);
setColor(mTimeRemaining,duration,colorDuration,Color.GRAY);
}
}
}
答案 0 :(得分:1)
我认为这是因为你正在循环中初始化“colorDuration”。试试这个。
private void updateDurationColor(SpinClassMovement movement){
String duration = (String) TextFormatUtil.getFormattedTimeInMinutesAndSeconds(movement.getMovementLengthInMinutes() + movement.getMovementLengthInSeconds());
StringBuilder colorDuration = new StringBuilder();
for(int i = 0; i < duration.length(); i++){
if (duration.charAt(i) == '0'){
Character zero = duration.charAt(i);
colorDuration.append(zero);
setColor(mTimeRemaining,duration,colorDuration,Color.GRAY);
}
}
}