我想做一个等待2秒的功能,然后做一些事情(显示文字)。
有没有办法做到这一点?感谢
我发布了代码,如果你能告诉我是否可能......
代码我想放的地方(已评论)
case (R.id.addPoints1):
if(restar){
pp1 = pp1 - 1;
playerPoints1.setText(Integer.toString(pp1));
restar = false;
} else {
pp1 = pp1 + 1;
playerPoints1.setText(Integer.toString(pp1));
}
if(pp1 == 10){
String messageString = "10 punts! Agafa la foto de la ciutat";
//Toast.makeText(getApplicationContext(), "10 punts! Agafa la foto de la ciutat", Toast.LENGTH_LONG);
Snackbar message = Snackbar.make(findViewById(R.id.myLinearLayout), messageString, Snackbar.LENGTH_LONG);
message.show();
pp1 = 0;
//WAIT 2 SECONDS
playerPoints1.setText(Integer.toString(pp1));
}
break;
case (R.id.addPoints2):
if(restar){
pp2 = pp2 - 1;
playerPoints2.setText(Integer.toString(pp2));
restar = false;
}else {
pp2 = pp2 + 1;
playerPoints2.setText(Integer.toString(pp2));
}
if(pp2 == 10){
String messageString = "10 punts! Agafa la foto de la ciutat";
//Toast.makeText(getApplicationContext(), "10 punts! Agafa la foto de la ciutat", Toast.LENGTH_LONG);
Snackbar message = Snackbar.make(findViewById(R.id.myLinearLayout), messageString, Snackbar.LENGTH_LONG);
message.show();
pp2 = 0;
//WAIT 2 SECONDS
playerPoints2.setText(Integer.toString(pp2));
}
break;
case (R.id.addPoints3):
if(restar){
pp3 = pp3 - 1;
playerPoints3.setText(Integer.toString(pp3));
restar = false;
}else {
pp3 = pp3 + 1;
playerPoints3.setText(Integer.toString(pp3));
}
if(pp3 == 10){
String messageString = "10 punts! Agafa la foto de la ciutat";
//Toast.makeText(getApplicationContext(), "10 punts! Agafa la foto de la ciutat", Toast.LENGTH_LONG);
Snackbar message = Snackbar.make(findViewById(R.id.myLinearLayout), messageString, Snackbar.LENGTH_LONG);
message.show();
pp3 = 0;
//WAIT 2 SECONDS
playerPoints3.setText(Integer.toString(pp3));
}
break;
case (R.id.addPoints4):
if(restar == true){
pp4 = pp4 - 1;
playerPoints4.setText(Integer.toString(pp4));
restar = false;
}else {
pp4 = pp4 + 1;
playerPoints4.setText(Integer.toString(pp4));
}
if(pp4 == 10){
String messageString = "10 punts! Agafa la foto de la ciutat";
//Toast.makeText(getApplicationContext(), "10 punts! Agafa la foto de la ciutat", Toast.LENGTH_LONG);
Snackbar message = Snackbar.make(findViewById(R.id.myLinearLayout), messageString, Snackbar.LENGTH_LONG);
message.show();
pp4 = 0;
//WAIT 2 SECONDS
playerPoints4.setText(Integer.toString(pp4));
}
break;
}
再次感谢!
答案 0 :(得分:3)
尝试这样的事情......
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// call your function
}
}, 2000);
答案 1 :(得分:2)
尝试CountDown Timer
:
new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();