Android 倒计时跳转页面之handler的详细运用

时间:2021-10-01 00:35:30

这是完整的一段代码 当然其中的按钮还需自己创建引用,希望能帮人帮己


public class MainActivity extends AppCompatActivity {

int count = 3;
Button btn_skip;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

getWindow().getDecorView().setSystemUiVisibility(View.GONE);//隐藏状态栏

setContentView(R.layout.activity_main);

btn_skip = (Button)findViewById(R.id.btn_skip);
btn_skip.setText(count+"s");

Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message,1000);

}

final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case 1:
count--;
btn_skip.setText(count+"s");

if (count > 0){
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message,1000);
}else{
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
}

super.handleMessage(msg);
}
};