MainActivity
public class MainActivity extends Activity {//定义一个变量
int item=5;
//全局变量
private Handler handler;
private TextView mtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
mtv = (TextView) findViewById(R.id.Mtv);
//实例化Hanlder
handler = new Handler();
//使用Hanlder发送延迟消息
handler.postDelayed(new Runnable() {
@Override
public void run() {
// 让item--
item--;
//判断
if (item==1) {
//item==1的时候就跳转
Intent intent=new Intent(MainActivity.this, Other.class);
startActivity(intent);
}
//调用自身发送延迟消息
handler.postDelayed(this,2000);
//给控件赋值
mtv.setText("倒计时"+item+"秒");
}
},2000);
}
}