如何显示然后删除android进度对话框

时间:2010-12-08 13:44:24

标签: android progressdialog

我可以使用以下代码显示进度条

pd = ProgressDialog.show(myActivity.this, "", "Loading. Please wait...", true);

直截了当,但是一旦我执行了代码,我希望它能够消失,但是当我从未看到对话框显示之后运行dismiss方法时。

下面包含在oncreate中的代码

pd = ProgressDialog.show(myActivity.this, "", "Loading. Please wait...", true);
runCode();  
setListAdapter(new CustomAdapter(myActivity.this)); 
pd.dismiss();

我认为你可以在活动的任何地方显示/解除进度对话框,但我一定是错的。

3 个答案:

答案 0 :(得分:11)

这是我开始工作的代码

private class UpdateFeedTask extends AsyncTask<MyActivity, Void, Void> {

    private ProgressDialog mDialog;

    protected void onPreExecute() {
        Log.d(TAG, " pre execute async");
        mDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);

    }

    protected void onProgressUpdate(Void... progress) {
        Log.d(TAG, " progress async");     
    }

    @Override
    protected Void doInBackground(MyActivity... params) {
        // TODO Auto-generated method stub
        return null;
    }

    protected void onPostExecute(Void result) {
        Log.d(TAG, " post execute async");
        mDialog.dismiss();
    }

}

答案 1 :(得分:2)

参考我的示例代码sinnpet,希望这可以帮助你

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ProgressBar;

public class PlayActivity extends Activity {
    /** Called when the activity is first created. */

  private ProgressBar mProgress;
     private int mProgressStatus = 0;
     private int count=0;
     private Handler mHandler=new Handler();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mProgress = (ProgressBar) findViewById(R.id.ProgressBar01);
        new Thread(myThread).start();

    }
    private Runnable myThread = new Runnable(){

     @Override
     public void run() {
     // TODO Auto-generated method stub
     while (mProgressStatus<=100){
     try{
     myHandle.sendMessage(myHandle.obtainMessage());
     Thread.sleep(1000);
     }
     catch(Throwable t){
     }
     }
     }

     Handler myHandle = new Handler(){

     @Override
     public void handleMessage(Message msg) {
     // TODO Auto-generated method stub
      mProgressStatus=mProgressStatus+10;
      count=mProgressStatus;
      Log.d("mProgressStatus",Integer.toString(count));
      mProgress.setProgress(mProgressStatus);
      if(count > 80)
            {
             Log.d("mProgressStatus",Integer.toString(mProgressStatus));
             counter.start();
            }
     }
     };
     };



}

答案 2 :(得分:-3)

删除progressBar

ProgressBar progressbar = (ProgressBar)findViewById(R.id.progressbarID);

progressBar.setVisibility(View.INVISIBLE) ;

隐藏xml中的进度条并将其显示为

 ProgressBar progressbar = (ProgressBar)findViewById(R.id.progressbarID);

    progressBar.setVisibility(View.VISIBLE) ;