【问题标题】:New developer - basic java question involving Timer implementation新开发人员 - 涉及 Timer 实现的基本 java 问题
【发布时间】: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 是我的应用程序的活动 - 顶级...

标签: java android timer


【解决方案1】:

如果 TimerButtons 是您的 Activity,那应该可以。试试这个:

TimerButtons.this.postDelayed(this, 1000);

编辑(为后代):我最初错了:postDelayed 是在 View 上定义的,而不是 Context。使用

mDisplayTime.postDelayed(this, 1000);

【讨论】:

  • 刚试过,但同样的错误。旧线程谈到创建一个处理程序,但我也迷路了。
  • 提供一个代码示例来展示周围的方法:如果这不起作用,很难说到底发生了什么。
  • 讨厌发布整个代码;也许这会有所帮助:我最初发布的代码是一个完整的方法,最后一个是在 onCreate 块结束之后(外部)创建的。戴夫
  • @Femi 和其他人:编辑帖子以包含整个应用程序。我删除了很多按钮着色等,但我相信整个结构代表了我正在尝试做的事情。戴夫
  • 啊。您需要对活动中的视图执行postDelayed 调用。试试mDisplayTime.postDelayed
【解决方案2】:

我认为你有 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);
        }


}  

我实现的类只是一个概念。我没有测试但编译类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-25
    • 2010-12-20
    • 1970-01-01
    • 2011-01-11
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多