ProgressDialog尽管被取消和隐藏,但仍然持续显示。

huangapple go评论64阅读模式
英文:

ProgressDialog keeps on showing despite being cancelled and hidden

问题

现在我正在模拟显示一个预计需要几秒钟的事件的 ProgressDialog。

我是这样做的:

progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage(getString(R.string.calendar_load));
progressDialog.setCancelable(false);
progressDialog.show();
Thread t = new Thread(new Runnable() {
    @Override
    public void run() {

        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                progressDialog.cancel();
                progressDialog.hide();

但是,即使我在调试中检查了 progressDialog.cancel() 和 progressDialog.hide() 的执行,对话框似乎仍然以一种不明确的方式持续显示。

是什么原因导致了这种行为?

问题已解决:感谢所有回答/评论的人,看起来是模拟器的错误(事实上,有时它在模拟器上也能工作)。

英文:

Right now I'm simulating the showing of a ProgressDialog for an event that is expected to take several seconds.

I'm doing it this way:

progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage(getString(R.string.calendar_load));
progressDialog.setCancelable(false);
progressDialog.show();
Thread t=new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                getActivity().runOnUiThread(new Runnable()
                {
                    @Override
                    public void run() {

                        progressDialog.cancel();
                        progressDialog.hide();

But even though I've checked in debug that the progressDialog.cancel() and progressDialog.hide() execute the dialog just keeps on showing apparently in an indefinite way.

What could be causing such behavior?

PROBLEM SOLVED: Thanks to everyone who has answered/commented, it looks like an emulator bug (indeed it has also worked some times on emulator).

答案1

得分: 2

调用 progressDialog.dismiss();

英文:

Call progressDialog.dismiss();

答案2

得分: 1

你能尝试这个代码片段吗?

progressDialog.show();
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            while (progressDialog.getProgress() <= progressDialog.getMax()) {
                Thread.sleep(100);
                handle.sendMessage(handle.obtainMessage());
                if (progressDialog.getProgress() == progressDialog.getMax()) {
                    progressDialog.dismiss();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}).start();

Handler handle = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        progressDialog.incrementProgressBy(1);
    }
};
英文:

Could you try this snippet?

progressDialog.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (progressDialog.getProgress() &lt;= progressDialog.getMax()) {
                            Thread.sleep(100);
                            handle.sendMessage(handle.obtainMessage());
                            if (progressDialog.getProgress() == progressDialog.getMax()) {
                                progressDialog.dismiss();
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            Handler handle = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        progressDialog.incrementProgressBy(1);
                    }
            };

huangapple
  • 本文由 发表于 2020年8月10日 17:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定