Android:是否会延迟烤面包的显示?

问题描述:

如果某个条件为 true false ,我希望显示 toast .但是,我希望此烤面包在显示之前先延迟两秒钟.

I wish to display a toast if a certain condition is true or false. However I want this toast to delay for two seconds before it is displayed.

我该怎么做?

当前的if语句:

if (result.equals("true")) {

                        loginDataBaseAdapter.updateUploadedRecord(sessionId);

                            Toast.makeText(MathsGameResults.this,
                                    "Data is successfully uploaded.",
                                    Toast.LENGTH_LONG).show();

                        } else {
                            Toast.makeText(
                                    MathsGameResults.this,
                                    "Error while uploading. Please try again later.",
                                    Toast.LENGTH_LONG).show();
                        }

                    }

尝试一下..

使用 Handler

            // Handler which will run after 2 seconds.
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                      Toast.makeText(MathsGameResults.this,
                                "Data is successfully uploaded.",
                                Toast.LENGTH_LONG).show();
                }
            }, 2000);