Hanlder倒计时跳转

来源:互联网 发布:广州做淘宝最多的地方 编辑:程序博客网 时间:2024/05/18 19:22

Hanlder倒计时跳转

原创 2017年08月24日 19:31:23

 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);
    }
 
}
原创粉丝点击