倒计时 3..2..1 跳转 hander消息机制

来源:互联网 发布:微信清理僵尸粉源码 编辑:程序博客网 时间:2024/06/14 05:10
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/logo"    tools:context="com.example.administrator.jinritoutiao.MainActivity"><TextView    android:id="@+id/tv"    android:layout_width="50dp"    android:layout_height="150dp"    android:textSize="40dp"    android:text="3"    android:textColor="#f00"    android:layout_alignParentRight="true"    />
//代码


public class MainActivity extends AppCompatActivity {
private Timer timer;
private int i = 3;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == 0){
//计时停止
timer.cancel();
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}else{
textView.setText(""+i--);
} } };
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
timer = new Timer();
textView = (TextView) findViewById(R.id.tv);
timer.schedule(new TimerTask() {
@Override
public void run() {
if(i == 0){
handler.sendEmptyMessage(0);
}else{
handler.sendEmptyMessage(1);
} } },0,1000); }}
原创粉丝点击