作者kkeeennnn (阿康)
看板AndroidDev
標題[問題] Android的程式裡面加上倒數計時的功能
時間Wed Dec 14 22:51:04 2011
各位好,我是初學android的學生
我想要寫一個簡單的限時答題程式
但是我不會倒數計時的功能
我有上網找過一篇教學
http://tinyurl.com/6s2jqvx
但是編譯一直不會過,
目前我修的程式碼如下:
package demo.game;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyCount extends CountDownTimer {
TextView timeDisplay;
MyCount counter;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timer);
timeDisplay = (TextView) findViewById(R.id.timer);
counter = new MyCount(600000, 1000);}
private TextView findViewById(int timer) {
// TODO Auto-generated method stub
return null;
}
private MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
timeDisplay.setText("done!");
}
public void onTick(long millisUntilFinished) {
timeDisplay.setText("Left: " + millisUntilFinished / 1000);
}
}
另外我的圖形介面的程式碼是這樣
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:text="TextView"
android:id="@+id/timer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:onClick="start" />
counter.start();
</LinearLayout>
但是紅字那邊會錯,請問有人可以稍微指點嗎?謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.152.187
推 ericinttu:你這個.layout檔 沒有叉叉嗎? 12/14 22:55
推 No:你把CountDownTimer當成Activity在繼承了 12/15 03:24
→ No:上面改回繼承Activity後,code gen出來的findViewById也要刪掉 12/15 03:25
→ No:照你參考的,應該是用inner class去繼承CountDownTimer 12/15 03:26
→ No:也就是你下方應該用類別定義的MyCount 12/15 03:26
→ No:另外layout裡面的counter.start();應該是寫在java code 12/15 03:27
→ kkeeennnn:感謝樓上兩位指點,我馬上來研究 12/15 16:23
→ PizzaCold345:試試AlarmManager 12/29 22:50