-2

当某些功能在后台运行时,我想显示一个圆形进度对话框。所以我写了这么一系列代码:

    pd = ProgressDialog.show(GridLayoutActivity.this, "","...");
    new Thread(new Runnable() {
    public void run() {
        someFunc();
        handler.sendEmptyMessage(0);// 执行耗时的方法之后发送消给handler
    }
    }).start();

但是progressDialog 不滚动?为什么?

4

3 回答 3

2

尝试使用异步任务,这里我发布示例代码,您可以根据自己的需要进行更改

public class BackGround_Task extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(
                YourClassName.this);

        // can use UI thread here
        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            //Your Background Task

            return null;
        }

        protected void onPostExecute(Void result) {

            //What will you do after the completion of Background process

            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
        }
    }
于 2012-09-04T08:53:25.257 回答
0

只需使用这样的东西:

ProgressDialog progressDialog = new ProgressDialog(GridLayoutActivity.this);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.setMessage("...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();

new Thread(new Runnable() {
public void run() {
    someFunc();
    handler.sendEmptyMessage(0);// 执行耗时的方法之后发送消给handler
}
}).start();
于 2012-09-04T08:52:46.587 回答
0

在 try 块之后发送处理程序消息。像这样使用:

pd = ProgressDialog.show(SetFrames.this,"Loading", "Please Wait...",true);
           new Thread() {
               public void run() {
               try {
                 //your function
               }catch(Exception e)
               {
               }
               handler.sendEmptyMessage(0);
               pd.dismiss();
               }

               }.start();

private Handler handler = new Handler() {
        public void handleMessage(Message msg) {

        }
        };
于 2012-09-04T09:03:02.493 回答