使用Timer运行方法

时间:2015-04-28 15:31:10

标签: java android methods timer timertask

我有一个包含一些findViewById的void方法,我想每秒运行一次。我怎样才能做到这一点? 我想出的就是:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  timer.schedule(new TimerTask() {
    public void run() {
       Method();
    }
  }, 1000, 1000);
}

public void Method() {
  findViewById(R.id.textView1).setVisibility(View.INVISIBLE);
  ((TextView) findViewById(R.id.textView1)).setText("A second has passed!");
}

1 个答案:

答案 0 :(得分:0)

将业务逻辑插入run方法。这将被称为。

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  timer.schedule(new TimerTask() {
    public void run() {
      findViewById(R.id.textView1).setVisibility(View.INVISIBLE);
  ((TextView) findViewById(R.id.textView1)).setText("A second has passed!");
    }
  }, 1000, 1000);
}