2015-01-27 14 views
-2

카운트 다운 타이머를 만들려고합니다. 텍스트 편집 속성에서 시간을 설정하고 타이머 (버튼)를 클릭 한 후 레이블로 보내면 카운트 다운이 시작됩니다. 나는이 부분에 대해 알아 봤지만 초 카운트 다운을 만드는 방법을 찾지 못했다. 만약 당신들 중 누구라도 도와 주시면 감사하겠습니다.델파이 카운트 다운 타이머

온라인에서 찾은 예제에서 이것을 시도했지만 Firemonkey 응용 프로그램이므로 작업을하지 못했습니다.

dec(TotalTime); {decrement the total time counter} 


// Timer code.. 
procedure TForm1.ButtonSetTimerClick(Sender: TObject); 
var 
GetTime : TDateTime; 
begin 
Timer3.Enabled := True; 
Label11.Text := Edit1.Text; 
ButtonSetTimer.Enabled := False; 
Edit1.Enabled := False; 
GetTime := StrToTime(Edit1.Text); 

end; 

procedure TForm1.ButtonStopTimerClick(Sender: TObject); 
begin 
Timer3.Enabled := False; 
ButtonSetTimer.Enabled := True; 
Edit1.Enabled := True; 
end; 

procedure TForm1.Timer3Timer(Sender: TObject); 
var 
GetTime : TDateTime; 
Hour, Min, Sec, MSec: Word; 
begin 

DecodeTime(GetTime, Hour, Min, Sec, Msec); 
Label11.Text := TimeToStr(GetTime); 
Label11.Text := IntToStr(Hour) + ':'+ IntToStr(Min) + ':'+ IntToStr(Sec); 
Label11.Text := Format('%2.2u:%2.2u:%2.2u',[Hour,Min,Sec]); 
end; 

건배.

+0

'countdowntimer' 태그를 제거했습니다. 태그 설명에 Android countdowntimer 클래스에만 적용된다는 것이 명확하게 명시되어 있으며 해당 클래스와 직접적으로 관련이없는 질문에 ** 태그를 ** 사용하지 말아야한다고 명시되어 있기 때문입니다. 태그 설명을 사용하기 전에 태그 설명을 읽은 다음 질문에 실제로 적용되는 태그 만 적용하고 유사한 태그 나 단어를 인식하기 때문에 기존 태그를 추가하지 마십시오. 태그에는 * 구체적인 * 의미가 있습니다. –

+0

또한 해당 코드의 어떤 부분이 FireMonkey에서 작동하지 않습니까? FMX는'TTimer' 클래스를 포함하고 있으며 모든 날짜/시간 관련 함수는 RTL의 일부이며 플랫폼에 종속적이지 않습니다. –

답변

1

당신은 (어떤 형식) 시간이은 TEdit에 입력하는 방법을 언급하지 않았다, 그래서 여기에 세 가지 다른 시간의 진입 가능성이 있습니다. 출력은 H : M : S 형식으로 출력됩니다.

오류를 잡기 위해 TryStrToInt/TryStrToTime을 사용하도록 어제 코드를 수정했습니다. 또한 Seconds 카운터는 앞의 예제 에서처럼 OnTimer 이벤트와 함께 정확도가 낮으며 5 분 이내에 몇 초 동안 표류 할 수 있습니다. Now과 계산 된 종료 시간을 비교하는 Edijs 솔루션은 OnTimer 이벤트의 부정확성에 영향을받지 않으므로이를 채택했습니다.

var 
    TimeOut: TDateTime; 

function SecsToHmsStr(ASecs: integer):string; 
begin 
    Result := Format('%2d:%2.2d:%2.2d', 
    [ASecs div 3600, ASecs mod 3600 div 60, ASecs mod 3600 mod 60]); 
;end; 

procedure TForm6.Timer1Timer(Sender: TObject); 
begin 
    Label1.Caption := SecsToHmsStr(SecondsBetween(Now, TimeOut)); 
    if Now > Timeout then Timer1.Enabled := False; 
end; 

타임 엔트리 대체 한 초 소정 횟수의 제한 시간

// Timeout after a given number of seconds 
procedure TForm6.Button1Click(Sender: TObject); 
var 
    Seconds: integer; 
begin 
    if TryStrToInt(Edit1.Text, Seconds) then 
    begin 
    TimeOut := IncSecond(Now, Seconds); 
    Timer1.Enabled := True; 
    Label1.Caption := SecsToHmsStr(SecondsBetween(Now, TimeOut)); 
    end 
    else 
    ShowMessage('Error in number of seconds'); 
end; 

타임 엔트리 두 대안, 소정 시간 분의 수 초

// Timeout after a given number of hours, minutes and seconds 
procedure TForm6.Button2Click(Sender: TObject); 
begin 
    if TryStrToTime(Edit1.Text, TimeOut) then 
    begin 
    TimeOut := Now + TimeOut; 
    Timer1.Enabled := True; 
    Label1.Caption := SecsToHmsStr(SecondsBetween(Now, TimeOut)); 
    end 
    else 
    ShowMessage('Error in time format'); 
end; 

시간 후 타임 아웃 항목 대체 3, 24 시간 내에 지정된 시간에 제한 시간

// Timeout at a given time within 24 hours 
procedure TForm6.Button3Click(Sender: TObject); 
begin 
    if TryStrToTime(Edit1.Text, TimeOut) then 
    begin 
    if TimeOut <= Time then 
     TimeOut := Tomorrow + TimeOut 
    else 
     TimeOut := Today + TimeOut; 
    Timer1.Enabled := True; 
    Label1.Caption := SecsToHmsStr(SecondsBetween(Now, TimeOut)); 
    end 
    else 
    ShowMessage('Error in time format'); 
end; 
0

이 그것을 수행해야합니다

Uses 
    System.DateUtils; 

type 
.. 
private 
    FDateTimeTo: TDateTime; 
end; 

function IntToTimeStr(const ASeconds: Int64): string; 
begin 
    Result := Format('%2d:%2.2d:%2.2d', [ASeconds div 3600, ASeconds mod 3600 div 60, 
    ASeconds mod 3600 mod 60]); 
end; 

procedure TForm1.BitBtn1Click(Sender: TObject); 
begin 
    FDateTimeTo := StrToDateTime(FormatDateTime('yyyy' + FormatSettings.DateSeparator + 'mm' + 
    FormatSettings.DateSeparator + 'dd 00:00:00', Now)) + StrToTime(Edit1.Text); 

    if CompareDateTime(Now, FDateTimeTo) = 1 then 
    FDateTimeTo := IncDay(FDateTimeTo); 
end; 

procedure TfrmMain.Timer1Timer(Sender: TObject); 
begin 
    Label1.Caption := IntToTimeStr(SecondsBetween(Now, FDateTimeTo)); 
end; 

The result

+0

Label1.Caption : = FloatToTimeStr (SecondsBetween (Now, _Later)); 나에게 연산자 또는 세미콜론 오류가 누락되었습니다. ' System.DateUtils, FloatToTimeStr : string;을 추가했습니다. 하지만 그것은 오직 하나의 오류만을 제공합니다. –

+0

잘 보이지 않습니다. eTime.Text가 초기 값인 경우 시간 0에 도달하지 않습니다. SecondsBetween은 확장되지 않고 Int64를 반환합니다. 초 단위에서 H : M : S 문자열로의 변환은 이러한 모든 문자열 변환을 앞뒤로 수행하는 것은 매우 비효율적입니다. "div"와 "mod"에 대해 들어 본 적이 있습니까? –

+0

Tom, 진단 도구를 사용했는지, 실제로 div를 사용하고 mod가 빠릅니다. –