Update TextView Every Second

前端 未结 9 1258
南旧
南旧 2020-11-28 23:58

I\'ve looked around and nothing seems to be working from what I\'ve tried so far...

    @Override
protected void onCreate(Bundle savedInstanceState) {
    su         


        
9条回答
  •  有刺的猬
    2020-11-29 00:16

    You can use Timer instead of Thread. This is whole my code

    package dk.tellwork.tellworklite.tabs;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import dk.tellwork.tellworklite.MainActivity;
    import dk.tellwork.tellworklite.R;
    
    @SuppressLint("HandlerLeak")
    public class HomeActivity extends Activity {
        Button chooseYourAcitivity, startBtn, stopBtn;
        TextView labelTimer;
        int passedSenconds;
        Boolean isActivityRunning = false;
        Timer timer;
        TimerTask timerTask;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tab_home);
    
            chooseYourAcitivity = (Button) findViewById(R.id.btnChooseYourActivity);
            chooseYourAcitivity.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //move to Activities tab
                    switchTabInActivity(1);
                }
            });
    
            labelTimer = (TextView)findViewById(R.id.labelTime);
            passedSenconds = 0;
    
            startBtn = (Button)findViewById(R.id.startBtn);
            startBtn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (isActivityRunning) {
                        //pause running activity
                        timer.cancel();
                        startBtn.setText(getString(R.string.homeStartBtn));
                        isActivityRunning = false;
                    } else {
                        reScheduleTimer();
                        startBtn.setText(getString(R.string.homePauseBtn));
                        isActivityRunning = true;
                    }
                }
            });
    
            stopBtn = (Button)findViewById(R.id.stopBtn);
            stopBtn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    timer.cancel();
                    passedSenconds = 0;
                    labelTimer.setText("00 : 00 : 00");
                    startBtn.setText(getString(R.string.homeStartBtn));
                    isActivityRunning = false;
                }
            });
        }
    
        public void reScheduleTimer(){
            timer = new Timer();
            timerTask = new myTimerTask();
            timer.schedule(timerTask, 0, 1000);
        }
    
        private class myTimerTask extends TimerTask{
            @Override
            public void run() {
                // TODO Auto-generated method stub
                passedSenconds++;
                updateLabel.sendEmptyMessage(0);
            }
        }
    
        private Handler updateLabel = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                //super.handleMessage(msg);
    
                int seconds = passedSenconds % 60;
                int minutes = (passedSenconds / 60) % 60;
                int hours = (passedSenconds / 3600);
                labelTimer.setText(String.format("%02d : %02d : %02d", hours, minutes, seconds));
            }
        };
    
        public void switchTabInActivity(int indexTabToSwitchTo){
            MainActivity parentActivity;
            parentActivity = (MainActivity) this.getParent();
            parentActivity.switchTab(indexTabToSwitchTo);
        }
    }
    

提交回复
热议问题