1

是否可以在 ProgressDialog 运行时显示 Toast?如果是,有一些关于如何做的例子吗?

谢谢。

我当前的代码不起作用:

final ProgressDialog pd = ProgressDialog.show(
                        BotonesServicio.this, "Medidas",
                        "Comprobando datos");
                new Thread(new Runnable() {
                    public void run() {
                    Toast.makeText(FacturasIFirmar.this,
                        "Trying to show toast", Toast.LENGTH_LONG)
                           .show();

                        pd.dismiss();
                    }
                }).start();
4

1 回答 1

2

ProgressDialog “冻结”线程,因此所有其他操作必须在单独的线程中执行。不过,您必须在 UI 线程上创建您的 toast。

尝试这样的事情:

    ProgressDialog dialog = new ProgressDialog(context);
    final Toast toast = Toast.makeText(context, "text", Toast.LENGTH_LONG);
    Thread thread = new Thread( new Runnable() {

        public void run() {
            //Calculations here
            try {

                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            toast.show();
        }

    });
    thread.start();
    dialog.show();

如果您想与 UI 线程通信,您应该使用 AsyncTask 或将消息发送到处理程序的常规线程,处理程序在 UI 线程上执行操作。

祝你好运!

于 2012-08-09T17:15:36.000 回答