Is it possible to wait until a toast has finished to resume the method?

女生的网名这么多〃 提交于 2019-12-23 16:51:34

问题


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();}
            }

回答1:


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.




回答2:


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.




回答3:


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();

}


来源:https://stackoverflow.com/questions/6163563/is-it-possible-to-wait-until-a-toast-has-finished-to-resume-the-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!