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

发布于 2024-10-07 06:38:01 字数 442 浏览 5 评论 0原文

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

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

,但是一旦执行代码,我希望它消失,但是当我在运行解雇方法后根本看不到对话框显示。

这是包含在 oncreate 中的上下文中的代码,

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

我认为您可以在活动中的任何位置显示/关闭进度对话框,但我一定是错的。

I can get a progress bar to appear with the following code

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

straight forward, but once I have the code execute I want it to go away, but when I run the dismiss method after I never see the dialog box show at all.

Heres the code in context which is wrapped in oncreate

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

I thought you can show/dismiss progress dialog's anywhere in the activity but I must be wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘心安 2024-10-14 06:38:01

这是我要工作的代码

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();
    }

}

here is the code that I got to work

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();
    }

}
じ违心 2024-10-14 06:38:01

参考我的示例代码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();
            }
     }
     };
     };



}

refer the sample code sinnpet of mine,hope this may help you

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();
            }
     }
     };
     };



}
北斗星光 2024-10-14 06:38:01

要删除进度条,

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

progressBar.setVisibility(View.INVISIBLE) ;

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

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

    progressBar.setVisibility(View.VISIBLE) ;

To remove a progressBar

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

progressBar.setVisibility(View.INVISIBLE) ;

hide the progressbar in xml and show it like

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

    progressBar.setVisibility(View.VISIBLE) ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文