2

有没有办法为进度条设置计时器?例如等待 10 秒然后关闭进度条?如果有人能给我一个简单的代码,我将不胜感激。

提前谢谢了 :)

4

3 回答 3

2

您可以在 UI-Thread 中创建一个处理程序,然后通过sendMessageDelayed调用它。

...
final Handler h = new Handler() {
  @Override
  public void handleMessage(Message message) {
    mProgressBar.dismiss();
  }     
};
h.sendMessageDelayed(new Message(), 10000);
...

此代码不是 testet。

于 2012-06-27T22:58:35.420 回答
1

使用处理程序类。

import android.os.Handler;
import android.os.Message;

public class someClass implements Handler.Callback {
    public static final int MSG_HIDE_PBAR = 0;
    static final long PBAR_DELAY = 10 * 1000; //Delay is is milliseconds

    //Probably should initialize this in your creation code (onCreate if activity)
    //and likewise set it to null when destroyed (onDestroy if activity)
    private Handler mHandler = new Handler(this); 

    public void showPbar(){
        //Show your progress bar here
        mHandler.sendEmptyMessageDelayed(MSG_HIDE_PBAR, PBAR_DELAY);
    }

    public boolean handleMessage(Message msg){
        switch (msg.what){
            case MSG_HIDE_PBAR:
                //hide your progress bar here, call postInvalidate instead of
                //invalidate because we are in a different thread
                break;
        }
        return true;
    }
}

快速:是,脏:是,完成工作:是

您可以使用带有睡眠调用或循环检查时钟的AsyncTask来完成相同的操作。Handler对我来说似乎更干净。

于 2012-06-27T22:54:23.143 回答
0

是的,您可以在 10 秒后使用ThreadrunOnUiThread解除:

public void myThread() {
    Thread th = new Thread() {
        @Override
        public void run() {
            try {
                while (mRunning) {
                    Thread.sleep(10L);//10s wait
                    YourCurrentActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            //DISMISS PROGRESS BAR HERE
                            mRunning = false;
                        }
                    });
                }
            } catch (InterruptedException e) {
                // TODO: handle exception
            }
        }
    };
    th.start();
}
于 2012-06-27T22:57:21.697 回答