我正在为自己构建计时器应用程序时学习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)方法。
感谢任何菜鸟指导!
戴夫
答案 0 :(得分:0)
如果TimerButtons是您的活动,那应该有效。试试这个:
TimerButtons.this.postDelayed(this, 1000);
编辑(后代):我最初错了:postDelayed
是在View
上定义的,而不是Context
。使用
mDisplayTime.postDelayed(this, 1000);
答案 1 :(得分:0)
我认为你有handler课来完成你的任务。
你的方法不同......
我不知道你真正想要这个班级做什么但是 这是一个实现。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import com.skens.sms.R;
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.os.Handler;
import android.os.SystemClock;
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;
private TinyTimer myTimer;
// 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();
myTimer.startTimer(1000, mDisplayTime, mTensDigit, mOnesDigit, mTenthsDigit);
}
});
mButtonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//timeTenths = 0;
//updateDisplay();
updateSetting();
}
});
myTimer = new TinyTimer();
// 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();
// }
// }
//};
public class TinyTimer {
private Handler _handler;
private long _startTime = 0;
private long _millis;
private long _delayMillis;
private TextView mDisplayTime;
private TextView mTensDigit;
private TextView mOnesDigit;
private TextView mTenthsDigit;
private String _format = String.format("%%0%dd", 2);
private int timeTenths = 0;
public TinyTimer(){
_handler = new Handler();
}
public void startTimer
(long delayMillis,TextView DisplayTime, TextView TensDigit, TextView OnesDigit, TextView TenthsDigit){
mDisplayTime = (TextView) DisplayTime;
mTensDigit = (TextView) TensDigit;
mOnesDigit = (TextView) OnesDigit;
mTenthsDigit = (TextView) TenthsDigit;
_delayMillis = delayMillis;
_startTime = SystemClock.elapsedRealtime();
_handler.removeCallbacks(updateTimerTask);
updateTimerTask.run();
}
public void stopTimer(){
_handler.removeCallbacks(updateTimerTask);
}
public void pauseTimer(){
_handler.removeCallbacks(updateTimerTask);
}
public void unpauseTimer(){
if(_startTime != 0){
updateTimerTask.run();
}
}
private Runnable updateTimerTask = new Runnable() {
@Override
public void run() {
final long start = _startTime;
_millis = SystemClock.elapsedRealtime() - start;
if (timeTenths >= 1)
{
timeTenths -= 1;
if (timeTenths != 0)
mDisplayTime.postDelayed(this, 100);
mDisplayTime.setText(String.valueOf(((float)timeTenths)/10));
mTensDigit.setText(String.valueOf(timeTenths/100));
mOnesDigit.setText(String.valueOf((timeTenths%100)/10));
mTenthsDigit.setText(String.valueOf(timeTenths%10));
}
_handler.postDelayed(updateTimerTask, _delayMillis);
}
};
public long getElapsedtime(){
return _millis;
}
public String getMillis(){
return String.format(_format, _millis % 1000);
}
}
我实施的课程只是一个概念。我没有测试但是编译类。