我正在学习android开发,现在我正在制作一个简单的计时器,我需要一个处理程序来使其保持运行状态。如果我将处理程序放在main活动中,那么所有东西都会出售,但是我想将方法​​放在main活动之外,这样我就可以重用代码,并且如果我想更改任何内容,应该更加灵活。

这是我的代码:

*public class Timer{

    public void runTimer(TextView view, int seconds, boolean running){
        TextView timerTextView = (TextView) view;
        Handler handler = new Handler();
        MyRunnable r = new MyRunnable(handler,view,seconds,running);
        handler.post(r);

    }



}

class MyRunnable implements Runnable {
    private TextView view;
    private int seconds;
    private boolean running;
    private Handler handler;

    public MyRunnable(Handler handler, TextView view, int seconds, boolean running){
        this.handler= handler;
        this.view =view;
        this.seconds = seconds;
        this.running = running;
    }

    public void run(){
        TextView timerTextView = (TextView) view;
        int hours = seconds/3600;
        int minutes = (seconds%3600)/60;
        int secs = seconds%60;
        String time = String.format("%d:%02d:%02d",hours,minutes,secs);
        timerTextView.setText(time);
        if (running){
            seconds++;
        }
        handler.postDelayed(this,100);
    }

}*


我在主活动中创建了一个对象来调用此函数。该应用程序可以运行,但计时器无法正常运行。它仅在第一次运行,然后完全停止。它不会一直运行。您知道问题出在哪里吗?首先感谢您的帮助!

主要代码:

*package com.mad.chapter4;

import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class StopWatchActivity extends AppCompatActivity {

    private Timer timer = new Timer();


    private int seconds = 0;
    private boolean running = false;
    private boolean wasRunning;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_watch);
        TextView ttv = (TextView) findViewById(R.id.timerTextView);

            if(savedInstanceState!=null){
                seconds = savedInstanceState.getInt("seconds");
                running = savedInstanceState.getBoolean("running");
                wasRunning = savedInstanceState.getBoolean("wasRunning");
            }

        seconds = timer.runTimer(ttv,seconds,running);
    }

    public void onSaveInstanceState (Bundle savedInstanceState){
        savedInstanceState.putInt("seconds",seconds);
        savedInstanceState.putBoolean("running",running);
        savedInstanceState.putBoolean("wasRunning",wasRunning);
    }

    protected void onStop(){
        super.onStop();
        wasRunning = running;
        running = false;
    }

    protected void onStart(){
        super.onStart();
        if(wasRunning){
            running = true;
        }
    }



    public void onClickStartButton(View view){
        running = true;
    }

    public void onClickStopButton (View view){
        running = false;
    }

    public void onClickResetButton (View view){
        seconds = 0;
        running = false;
    }



//    public void runTimer(){
//
//        final TextView timerTextView = (TextView) findViewById(R.id.timerTextView);
//        final Handler handler = new Handler();
//
//        Runnable r = new Runnable(){
//            @Override
//            public void run(){
//                int hours = seconds/3600;
//                int minutes = (seconds%3600)/60;
//                int secs = seconds%60;
//                String time = String.format("%d:%02d:%02d",hours,minutes,secs);
//                timerTextView.setText(time);
//                if (running){
//                    seconds++;
//                }
//                handler.postDelayed(this,100);
//            }
//        };
//
//        handler.post(r);
//
//
//    }


}*

最佳答案

从我的角度来看,您的runningwasRunning从未设置为true,这反过来并不会仅在第一次更新用户界面,而是在之后没有更新。在onCreate中调用running之前,请尝试将wasRunningtimer.runTimer(ttv, seconds, running)设置为true。我认为,当您在活动中调用此代码时,您正在调用onClickStartButton(View view),它确实将running标志设置为true,并且正在正确更新UI。

同样,您的runTimer函数不返回任何内容,但是您在代码中将此函数结果分配给int。那是复制/粘贴错误吗?

关于java - 无法在Android中正确使用处理程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37626827/

10-11 17:29