作者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
1F:推 ericinttu:你这个.layout档 没有叉叉吗? 12/14 22:55
2F:推 No:你把CountDownTimer当成Activity在继承了 12/15 03:24
3F:→ No:上面改回继承Activity後,code gen出来的findViewById也要删掉 12/15 03:25
4F:→ No:照你参考的,应该是用inner class去继承CountDownTimer 12/15 03:26
5F:→ No:也就是你下方应该用类别定义的MyCount 12/15 03:26
6F:→ No:另外layout里面的counter.start();应该是写在java code 12/15 03:27
7F:→ kkeeennnn:感谢楼上两位指点,我马上来研究 12/15 16:23
8F:→ PizzaCold345:试试AlarmManager 12/29 22:50