public class MainActivity extends Activity {
private TextView text_time;
private int time = 3;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
if (time > 0) {
time--;
text_time.setText(time + "s");
handler.sendEmptyMessageDelayed(0, 1000);
} else {
Intent intent = new Intent(MainActivity.this,
ShowActivity.class);
startActivity(intent);
finish();
}
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
text_time = (TextView) findViewById(R.id.text_time);
// 延时发送消息
handler.sendEmptyMessageDelayed(0, 1000);
}
/*
* 点击跳转
*/
public void tiao(View view) {
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
startActivity(intent);
handler.removeCallbacksAndMessages(null);
finish();
}
}