//Mainactivity
private TextView sd; int i = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); sd = (TextView) findViewById(R.id.sd); thr(); }
//接收的Handler Handler handler =new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); sd.setText("倒计时"+i); i--; thr(); if(i==-1){ Intent intent = new Intent(Main3.this, MainActivity.class); startActivity(intent); finish(); } } };
//线程的方法 public void thr(){ new Thread(){ @Override public void run() { super.run(); try { sleep(1000); handler.sendEmptyMessage(1); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); }; }
//布局XML
<TextView android:id="@+id/sd" android:text="倒计时3" android:textSize="80dp" android:layout_width="match_parent" android:layout_height="match_parent" />