這裏是我對格式的倒數計時器(MM:SS)中所討論的鏈接:在倒數計時器需要在Android
Java console code for StopWatch/Timer?
現在我想顯示(和更新)它在Java中android中的textview。任何線索?
這裏是我對格式的倒數計時器(MM:SS)中所討論的鏈接:在倒數計時器需要在Android
Java console code for StopWatch/Timer?
現在我想顯示(和更新)它在Java中android中的textview。任何線索?
倒計時
您將使用一個文本字段和更新使用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中設置它,我不認爲這是可能的。根據您的需要將它放在其右側或左側。
如果這不是你想要的,我希望它可以幫助別人。
您還可以使用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
}
}
更新您的TextView使用定時UI更新的方法,在這篇文章中它真正的偉大工程
http://developer.android.com/resources/articles/timed-ui-updates.html
此外,您可以使用內置的Android的時候類顯示你的文字,它會讓你的格式更容易IMO
http://developer.android.com/reference/android/text/format/Time.html
試試這個代碼.....
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);
}
知道了!謝謝。 – 2010-08-18 10:16:17
這不算* * *嗎? – Justin 2010-08-18 18:42:24
是的,這會算起來,我的錯誤,但它看起來像馬蘇德不介意:)我已經更新了答案。 – Maragues 2010-08-19 07:27:49