【发布时间】:2011-05-07 16:00:17
【问题描述】:
我正在学习 Android 和 java,同时为自己构建一个计时器应用程序。
引用一个旧线程,Android - Controlling a task with Timer and TimerTask?
我正在尝试创建 Runnable 方法来倒计时。
我坚持的基本 java 问题是我要附加 postDelayed() 调用什么类?
我的活动现在称为 TimerButtons,我认为这会起作用:
package com.TimerButtons;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TimerButtons extends Activity {
private TextView mDisplayTime;
private Button mButtonStart;
private Button mButtonStop;
private int timeTenths = 0;
private Drawable d;
private PorterDuffColorFilter filter;
// capture our View elements
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
filter = new PorterDuffColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_ATOP);
d = findViewById(R.id.buttonStart).getBackground(); d.setColorFilter(filter);
d = findViewById(R.id.buttonStop).getBackground(); d.setColorFilter(filter);
mDisplayTime = (TextView) findViewById(R.id.displayTime);
mButtonStart = (Button) findViewById(R.id.buttonStart);
mButtonStop = (Button) findViewById(R.id.buttonStop);
// add click listeners to the buttons
mButtonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Thread(r).start();
}
});
mButtonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
timeTenths = 0;
updateDisplay();
updateSetting();
}
});
// display the current time
updateDisplay();
}
// runtime methods below here
// updates the time we display in the TextView
private void updateDisplay() {
mDisplayTime.setText(
String.valueOf(((float)timeTenths)/10)
);
}
private void updateSetting() {
mTensDigit.setText(String.valueOf(timeTenths/100));
mOnesDigit.setText(String.valueOf((timeTenths%100)/10));
mTenthsDigit.setText(String.valueOf(timeTenths%10));
}
Runnable r = new Runnable()
{
public void run()
{
if (timeTenths >= 1)
{
timeTenths -= 1;
if (timeTenths != 0)
mDisplayTime.postDelayed(this, 100);
updateDisplay();
}
}
};
}
我收到错误:注释行上的 TimerButtons 类型的方法 postDelayed(new Runnable(){}, int) 未定义。
感谢任何菜鸟的指导!
戴夫
【问题讨论】:
-
TimerButtons 是我的应用程序的活动 - 顶级...