1. 程式人生 > >三秒跳轉

三秒跳轉

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();
    }
}