计时器android app启动创建活动

时间:2015-11-10 22:20:37

标签: android

我很久以前写过这段代码

公共类MainActivity扩展了Activity {

 private Button startButton;     
    private Button stopButton;  
    private Button clearButton;
    private TextView timeValue;     
    private long timeStart = 0L;
    private Handler timeHandler = new Handler();
    long timeInMilisec = 0L;
    long timeMemo = 0L;
    long timeUpdate = 0L;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     timeValue = (TextView) findViewById(R.id.timeValue);            

        startButton = (Button) findViewById(R.id.startButton);      
        startButton.setOnClickListener(new View.OnClickListener() { 

            public void onClick(View view) {

                timeStart = SystemClock.uptimeMillis();     
                timeHandler.postDelayed(updateTimerThread, 0); 
            }
        });

        stopButton = (Button) findViewById(R.id.stopButton);        
        stopButton.setOnClickListener(new View.OnClickListener() {       

            public void onClick(View view) {        
                timeMemo += timeInMilisec;      
                timeHandler.removeCallbacks(updateTimerThread); 
            }
        });


     clearButton = (Button) findViewById(R.id.clearButton);
     clearButton.setOnClickListener(new View.OnClickListener() {                
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.clearButton:

                timeMemo = 0L;                  
                timeValue.setText("00:00:00");
                int secs = 0;       
                int mins = 0;       
                secs = 0;   
                int milliseconds = 0;
                timeInMilisec = SystemClock.uptimeMillis() - timeStart;     
                timeUpdate = timeMemo + timeInMilisec;      
                timeValue.setText("" + mins + ":"       
                        + String.format("%02d", secs) + ":"     
                        + String.format("%03d", milliseconds)); 

                timeHandler.removeCallbacksAndMessages(updateTimerThread);
                timeValue.setText("00:00:00");

                break;                  
            }                   
        }
    });

    }

     Runnable updateTimerThread = new Runnable() {       

        public void run() {

            timeInMilisec = SystemClock.uptimeMillis() - timeStart;     
            timeUpdate = timeMemo + timeInMilisec;      
            int secs = (int) (timeUpdate / 1000);       
            int mins = secs / 60;       
            secs = secs % 60;       
            int milliseconds = (int) (timeUpdate % 1000);       
            timeValue.setText("" + mins + ":"       
                    + String.format("%02d", secs) + ":"     
                    + String.format("%03d", milliseconds));     
            timeHandler.postDelayed(this, 0);              

        }

    };      

现在我希望我的计时器在活动开始时开始运行而不按下开始按钮(我想删除它) 我试图将开始按钮的代码放在public void run()的末尾,但它对我没用。如何在活动启动时启动计时器?

2 个答案:

答案 0 :(得分:1)

U可以模拟按下按钮调用onPerformClick()on onSesume()

@Override
public void onResume(){
  startButton.onPerformClick();
}

答案 1 :(得分:0)

只需将startButton onClick中的代码直接放入onCreate()方法即可。像这样......

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //////////////////////////////////////////////////////////
    ///Put your timer start code here ///////////////////
    /////////////////////////////////////////////////////////
    timeStart = SystemClock.uptimeMillis();     
    timeHandler.postDelayed(updateTimerThread, 0); 
    //////////////////////////////////////////////////////////

    timeValue = (TextView) findViewById(R.id.timeValue);     

    stopButton = (Button) findViewById(R.id.stopButton);        
    stopButton.setOnClickListener(new View.OnClickListener() {       

        public void onClick(View view) {        
            timeMemo += timeInMilisec;      
            timeHandler.removeCallbacks(updateTimerThread); 
        }
    });


     clearButton = (Button) findViewById(R.id.clearButton);
     clearButton.setOnClickListener(new View.OnClickListener() {                
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.clearButton:

                timeMemo = 0L;                  
                timeValue.setText("00:00:00");
                int secs = 0;       
                int mins = 0;       
                secs = 0;   
                int milliseconds = 0;
                timeInMilisec = SystemClock.uptimeMillis() - timeStart;     
                timeUpdate = timeMemo + timeInMilisec;      
                timeValue.setText("" + mins + ":"       
                        + String.format("%02d", secs) + ":"     
                        + String.format("%03d", milliseconds)); 

                timeHandler.removeCallbacksAndMessages(updateTimerThread);
                timeValue.setText("00:00:00");

                break;                  
            }                   
        }
    });

    }