Is it possible to wait until a toast has finished to resume the method?
In one of my methods, I have a toast
that appears if the user gives the correct input. However, I do not want the next image to display until the toast has finished.
If I use Thread.sleep(3000)
if does not allow the toast
to show as the UI activity is asleep.
An example of what I am trying to do:
public void correction(){
if(correctionBoolean 开发者_如何学编程== true){
Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();
if(Toast.time == finished){
NextImage();}
}
I don't believe there would be any way to do this with a toast. If you are simply trying to show someone a "You're Correct" Window, I would consider simply using an AlertDialog with a single positive Okay button.
It would even be possible to show a dialog with no buttons, have a non-UI thread sleep for a bit and then dismiss the dialog.
Create a custom dialog with no buttons and use a handler to both dismiss it after a short time and then show the next image.
Use a CountDownTimer
with Toast.LENGTH_SHORT
as the time?
public void correction(){
if(correctionBoolean == true){
Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();
new CountdownTimer(Toast.LENGTH_SHORT, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
NextImage();
}
}.start();
}
精彩评论