Android: simple time counter

后端 未结 6 2236
-上瘾入骨i
-上瘾入骨i 2020-12-09 02:56

I have a simple program with one TextView and two Buttons: Button1 and Button2.

Clicking on Button 1 will start a counter, increasing by 1 every 1 second and show res

6条回答
  •  我在风中等你
    2020-12-09 03:46

    You can introduce flag. Something like isPaused. Trigger this flag whenever 'Pause' button pressed. Check flag value in your timer task. Success.

    Timer T=new Timer();
    T.scheduleAtFixedRate(new TimerTask() {         
            @Override
            public void run() {
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        myTextView.setText("count="+count);
                        count++;                
                    }
                });
            }
        }, 1000, 1000);
    
    
     onClick(View v)
     {
          //this is 'Pause' button click listener
          T.cancel();
     }
    

提交回复
热议问题