Android how to update value of ProgressBar?

后端 未结 4 1653
旧巷少年郎
旧巷少年郎 2021-01-12 01:46

My activity have a ProgressBar. When start activity, I\'ll check value get from another place and update to ProgressBar. Here\'s my code:

final ProgressBar p         


        
4条回答
  •  清酒与你
    2021-01-12 02:14

    I usually use this code:

    private int mProgressStatus;
    private Handler mHandler = new Handler();
    
    public void startProgress(View view) {
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.horizontal_progress);
        mProgressStatus = 0;
        progressBar.setProgress(mProgressStatus);
    
        //progressBar.setVisibility(View.VISIBLE);
        new AsyncTask() {
            @Override
            protected Void doInBackground(final Void... params) {
                while (mProgressStatus < 100) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mProgressStatus += 15;
                    mHandler.post(new Runnable() {
                        public void run() {
                            progressBar.setProgress(mProgressStatus);
                        }
                    });
                }
                return null;
            }
    
        }.execute();
    }
    

    Hope this helps

提交回复
热议问题