我正在學習Android和Java,同時爲自己構建計時器應用程序。
引用舊的線程Android - Controlling a task with Timer and TimerTask? 我想創建Runnable方法來倒計時我的計時器。
我堅持的基本的java問題是類怎麼附加postDelayed()調用?新的開發人員 - 涉及計時器實現的基本Java問題
我的活動被稱爲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();
}
}
};
}
我得到的錯誤:方法postDelayed(新的Runnable(){},INT)是未定義的類型TimerButtons在註釋行中。
感謝您的任何noob指導!
Dave
什麼是TimerButtons? – Varun 2011-05-07 16:14:09
TimerButtons是我的應用程序的活動 - 頂級... – DBell 2011-05-07 16:29:25