Android 倒计时进入主页面

public class HomeActivity extends AppCompatActivity {

private TextView text;
int time = 3;

Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (time>0){
            text.setText(time -- + " S");
            handler.sendEmptyMessageDelayed(100,1000);
        }else{
            Intent intent = new Intent(HomeActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    //获取id
    text = findViewById(R.id.textView);
    handler.sendEmptyMessage(100);
}

}

这是XML里面的textview
Android 倒计时进入主页面_第1张图片

你可能感兴趣的:(Android 倒计时进入主页面)