2010-08-18 82 views

回答

12

倒計時

您將使用一個文本字段和更新使用CountDownTimer其內容,或檢查拉胡爾的回答在這個同樣的問題。

要統計

在你的活動

import android.widget.Chronometer; 

... 

private Chronometer crono; 

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.screen_crono);  

this.crono = (Chronometer) findViewById(R.id.calling_crono); 

    startCrono(); 
} 

public void startCrono() { 
crono.setBase(SystemClock.elapsedRealtime()); 
crono.start(); 
} 

要停止它

crono.stop(); 

在XML佈局screen_crono

<Chronometer 
    android:id="@+id/calling_crono" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
    android:textStyle="bold" 
android:textSize="14sp"/> 

爲了在TextView中設置它,我不認爲這是可能的。根據您的需要將它放在其右側或左側。

如果這不是你想要的,我希望它可以幫助別人。

+0

知道了!謝謝。 – 2010-08-18 10:16:17

+0

這不算* * *嗎? – Justin 2010-08-18 18:42:24

+0

是的,這會算起來,我的錯誤,但它看起來像馬蘇德不介意:)我已經更新了答案。 – Maragues 2010-08-19 07:27:49

7

您還可以使用android中提供的CountDownTimer類。

只是聲明瞭一個構造函數,並與

timer test=new timer(30000,1000); 

onTick將在上述情況下每1000毫秒炒魷魚一旦開始計時。你可以從這裏

class timer extends CountDownTimer 
{ 

public timer(long millisInFuture, long countDownInterval) 
    { 
    super(millisInFuture, countDownInterval); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public void onFinish() 
    { 


} 

@Override 
public void onTick(long millisUntilFinished) 
    { 
    // TODO Auto-generated method stub 
      // Update your textview on on tick 

} 

} 
0

試試這個代碼.....

tv = new TextView(this); 
    this.setContentView(tv); 

//10000 is the starting number (in milliseconds) 
//1000 is the number to count down each time (in milliseconds) 
MyCount counter = new MyCount(10000,1000); 

counter.start(); 

} 

//countdowntimer is an abstract class, so extend it and fill in methods 
public class MyCount extends CountDownTimer 
{ 

public MyCount(long millisInFuture, long countDownInterval) 
{ 
super(millisInFuture, countDownInterval); 
} 

public void onFinish() 
{ 
tv.setText("Time Up!"); 
} 

public void onTick(long millisUntilFinished) 
{ 
tv.setText("Time Left : " + millisUntilFinished/1000); 

}