2014-01-09 27 views
1

这是我的代码:我怎么能叫定时器的方法?

package com.example.wackamole; 

import java.util.Timer; 
import java.util.TimerTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

static int scoreCount; 
static Timer t; 
static int count; 
static int timeInterval; 

    protected TextView textView, counter; 
    protected ImageButton button; 

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

    t = new Timer(); 

    scoreCount = 0; 
    count = 0; 
    timeInterval = 1000; 

    button = (ImageButton)findViewById(R.id.mole); 
    button.setBackgroundDrawable(null); 
    textView = (TextView)findViewById(R.id.score); 

    t.scheduleAtFixedRate(new TimerTask(){ 

    @Override 
    public void run() { 
     runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       if (count%2 == 0) 
        button.setImageResource(R.drawable.mole); 
       else 
        button.setImageResource(R.drawable.hole); 

       count++; 
       textView.setText("Score:" + count); 


      } 

     }); 

    } 
},0, timeInterval); 

    findViewById(R.id.mole).setOnClickListener(new View.OnClickListener() { 
      public void onClick (View v){ 


       if (timeInterval > 100) 
        timeInterval -= 50; 


       if (t != null){ 
        t.cancel(); 
        t.purge(); 
        t = null; 
       } 
      } 
      }); 

    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

我希望能够再次停止计时器,当我按一下按钮,然后重新启动定时器(此时,计时器间隔较小,因此会更快) 。这是像onCreate()?或onResume?如果是的话,有人可以向我解释这些吗?

+0

我不太清楚是什么问题。你能否让你的问题更清楚? – hichris123

回答

1

如果我理解你的权利,你要创建一个计时器,将被取消,在按下一个按钮复位。如果是这样,我认为这应该适合你。

外的你的onCreate,你可以创建处理定时器的取消和创建的方法:()似乎符合您的需求

private void setupTimer() { 
    t.cancel(); 
    t.schedule(new TimerTask() { 

     @Override 
     public void run() { 
      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        if (count % 2 == 0) 
         button.setImageResource(R.drawable.mole); 
        else 
         button.setImageResource(R.drawable.hole); 

        count++; 
        textView.setText("Score:" + count); 
       } 
      }); 
     } 
    }, timeInterval); 
} 

Timer.schedule。您可以阅读关于Timer提供的不同方法的更多信息:http://developer.android.com/reference/java/util/Timer.html

Timer.scheduleAtFixedRate()以给定间隔连续触发。该区间的速度是固定的,不会满足您的需求,以减少计时器的时间间隔每次按下按钮。

现在里面你的onCreate当用户点击按钮,就可以把这种新的方法:

findViewById(R.id.mole).setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      if (timeInterval > 100) 
       timeInterval -= 50; 

      setupTimer(); 
    } 
}); 

小号

+1

非常感谢回答! – Lightgod131

+0

没问题。很高兴我能帮上忙! –