How to stop Runnable on button click in Android?

前端 未结 5 1501
野趣味
野趣味 2020-12-11 18:44

I have to start runnable on start button click and stop it on pause button click. My code for start runnable on start button click is

    // TODO Auto-gener         


        
5条回答
  •  没有蜡笔的小新
    2020-12-11 19:09

    Thread thread;
    //inside start button

     thread=new Thread(new Runnable() {
    
        @Override
        public void run() {
        sec += 1;
            if(sec >= 60) {
                 sec = 0;
                 min += 1;
                if (min >= 60) {
                    min = 0;
                    hour += 1;
            }
             }
             Min_txtvw.setText(String.format(mTimeFormat, hour, min, sec));
             mHandler.postDelayed(mUpdateTime, 1000);
          });
       thread.start();
    

    //inside stop button

    mHandler.removeCallbacksAndMessages(runnable);
    thread.stop();
    

提交回复
热议问题