5秒倒计时
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textSize="20sp"
android:layout_centerInParent="true"
/>
private int count = 5;
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
String str[] = new String[]{"1","2","3","4","5"};
tv_text.setText(str[count]);
if (count==0){
Intent intent = new Intent(MainActivity.this,ItemActivity.class);
startActivity(intent);
}
return false;
}
});
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = findViewById(R.id.tv_text);
new Thread(new Runnable() {
@Override
public void run() {
while (count>0){
try {
Thread.sleep(1000);
handler.sendEmptyMessage(0);
count--;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}