2013-01-02 4 views
0
내 응용 프로그램에서 타이머 표시하려면 다음 코드를 사용하고

("시간"이라는 글고 인치)의 서식을 변경는 시간 표시

final TextView time = (TextView) findViewById(R.id.txtTime); 
Long spentTime = System.currentTimeMillis() - startTime; 
Long minius = (spentTime/1000)/60; 
Long seconds = (spentTime/1000) % 60; 
time.setText(minius+":"+seconds); 
handler.postDelayed(this, 1000); 

이 잘 작동하지만 서식 조금 추한입니다. 예를 들어 4 초에 타이머는 실제로 타이머처럼 보이지 않는 "0 : 4"를 보여줍니다.

if 문을 여러 번 만들 필요없이 더 일반적인 타이머 (예 : 00:04)를 표시하도록 setText의 서식을 변경하려면 어떻게해야합니까?

답변

1

)에 당신은 0 년대와 패드에 숫자를 String.format를 사용할 수 있습니다

String formattedTime = String.format("%02d:%02d", minius, seconds); 
time.setText(formattedTime); 
0

당신은 String.format 명령으로 공의를 선도하는 설정해야합니다. 시도해보십시오 :

time.setText(String.format("%02d",minius) + ":" + String.format("%02d",seconds)); 

이렇게하면 단 하나 일 때 항상 0 자리로 이어집니다.

+0

끝 부분에 한 괄호가 없습니다. –

+0

@MartinMilesich - 캐치를 가져 주셔서 감사합니다. –