【问题标题】:Why my TOAST is showing twice in post handler?为什么我的 TOAST 在 post 处理程序中显示两次?
【发布时间】:2021-12-24 10:35:08
【问题描述】:

我只想在progressStatus达到100时显示我的toast消息。为什么它执行了两次?感谢您的友好回复。

new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (progressStatus < 100) {
                                // Update the progress status
                                progressStatus += 1;

                                // Try to sleep the thread for 50 milliseconds
                                try {
                                    Thread.sleep(50);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }       
                                handler.post(new Runnable(){

                                        @Override
                                        public void run() {
                                            pb = dialog.findViewById(R.id.no_connectiondProgressBar);
                                            pb.setProgress(progressStatus);

                                            if (progressStatus == 100) {
                                                
                                                final Toast toast = new Toast(getApplicationContext());
                                                toast.setDuration(Toast.LENGTH_LONG);
                                                View custom_view = getLayoutInflater().inflate(R.layout.custom_toast, null);
                                                toast.setView(custom_view);
                                                toast.show(); 
                                                dialog.dismiss();

                                            }
                                        }


                                    });
                            }
                        }
                    })

【问题讨论】:

    标签: java android android-toast


    【解决方案1】:

    将 handler.post() 移到 while 循环之外

            new Thread(new Runnable() {
            @Override
            public void run() {
                
                while (progressStatus < 100) {
                    // Update the progress status
                    progressStatus += 1;
    
                    // Try to sleep the thread for 50 milliseconds
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                }
                handler.post(new Runnable(){
    
                    @Override
                    public void run() {
                        pb = dialog.findViewById(R.id.no_connectiondProgressBar);
                        pb.setProgress(progressStatus);
    
                        if (progressStatus == 100) {
    
                            final Toast toast = new Toast(getApplicationContext());
                            toast.setDuration(Toast.LENGTH_LONG);
                            View custom_view = getLayoutInflater().inflate(R.layout.custom_toast, null);
                            toast.setView(custom_view);
                            toast.show();
                            dialog.dismiss();
    
                        }
                    }
    
    
                });
            }
        })
    

    【讨论】:

    • 先生,谢谢你纠正我。
    • @JinAppsdev 如果我的回答解决了您的问题,您可以将其标记为已接受的答案。所以未来有同样问题的访问者会知道它有一个有效的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多