android 代码循环,Android – 每5秒循环一部分代码

我想按下START按钮开始重复两行代码,然后按下按钮STOP.我尝试使用TimerTask和Handles,但无法弄清楚如何.

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//final int i;

final TextView textView = (TextView) findViewById(R.id.textView);

final Button START_STOP = (Button) findViewById(R.id.START_STOP);

final ImageView random_note = (ImageView) findViewById(R.id.random_note);

final int min = 0;

final int max = 2;

final Integer[] image = { R.drawable.a0, R.drawable.a1,R.drawable.a2 };

START_STOP.setTag(1);

START_STOP.setText("START");

START_STOP.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int status = (Integer) v.getTag();

if (status ==1) {

textView.setText("Hello");

START_STOP.setText("STOP");

v.setTag(0);

final Random random = new Random();

//************************************************************

// I would like to loop next 2 lines of code every 5 seconds.//

int i = random.nextInt(2 - 0 + 1) + 0;

random_note.setImageResource(image[i]);

//************************************************************

}

else

{

textView.setText("Bye");

START_STOP.setText("Let's PLAY!");

v.setTag(1);

}

}

});

}

@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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值