使用带处理程序的进度条

时间:2014-02-16 10:35:44

标签: java android handler runnable android-progressbar

有没有办法使用带处理程序的进度条?我有下面的代码,我想包括一个进度条,它将“倒计时”延迟到下一个循环。 (在这种情况下是2000毫秒)。

private boolean started = false;
private Handler handler = new Handler();

private Runnable runnable = new Runnable() {        
    @Override
    public void run() {
        final Random random = new Random();
        int i = random.nextInt(2 - 0 + 1) + 0;
        random_note.setImageResource(image[i]);
        if(started) {
            start();
        }
    }
};

public void stop() {
    started = false;
    handler.removeCallbacks(runnable);
}

public void start() {
    started = true;
    handler.postDelayed(runnable, 2000);        
}

3 个答案:

答案 0 :(得分:0)

班级:

ProgressDialog asycdialog;

定义(在onCreate()中):

asycdialog = new ProgressDialog(YourActivity.this);
asycdialog.setMessage("Working");
asycdialog.getWindow().setGravity(Gravity.CENTER_VERTICAL);
asycdialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
asycdialog.setCancelable(false);
在你的开始方法中

asycdialog.show();
在你的停止方法中

asycdialog.dismiss();

答案 1 :(得分:0)

我真的不明白你的意思但你可以用这种方式处理程序来从backGroud线程升级progressBar

private ProgressBar mProgressBar
    private Handler handle = new Handler(){

    @Override
    public  void handleMessage(Message msg){

        super.handleMessage(msg);
        mProgressBar.setProgress(msg.arg1);

    }



};

并从另一个线程发送更新

Message msg = handle.obtainMessage(null,intWhat, mProgressionValur, 0);
                      handle.sendMessage(msg);

答案 2 :(得分:0)

在一段时间后显示进度并解散:

 mProgressDialog = new ProgressDialog(getActivity());
 mProgressDialog.setMessage("loading info...");
 mProgressDialog.setCancelable(false);
 mProgressDialog.setCanceledOnTouchOutside(false);
 mProgressDialog.setIndeterminate(true);
 mProgressDialog.setProgressStyle(android.R.attr.progressBarStyleSmall);
 mProgressDialog.show();

   new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.dismiss();
            }
        }, 3000);