简体   繁体   English

android中的进度条更新

[英]progress bar update in android

I'm developing an android application. 我正在开发一个android应用程序。 Working on vertical progress bar. 在垂直进度条上工作。
When I add new item, the item is associated with some percentage value say 30%. 当我添加新项目时,该项目与某个百分比值(例如30%)相关联。
The progress bar need to update from 0% to 30%. 进度条需要从0%更新到30%。 And if I add another item with percentage value say 40%, the progress bar need to update from 30% to 70%(30+40) . 如果我添加另一个项目,其百分比值为40%,则进度条需要update from 30% to 70%(30+40)
How can I do this. 我怎样才能做到这一点。 Sample code would be helpful. 示例代码会有所帮助。 I have posted my code here. 我已经在这里发布了我的代码。

public class VerticalProgressBar extends Activity {

Button buttonStart;
ProgressBar vProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verticalprogressbar);
    buttonStart = (Button) findViewById(R.id.start);
    vProgressBar = (ProgressBar) findViewById(R.id.vprogressbar);

    buttonStart.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            buttonStart.setClickable(false);
            new asyncTaskUpdateProgress().execute();
        }
    });

}

public class asyncTaskUpdateProgress extends AsyncTask<Void, Integer, Void> {

    int progress;

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        buttonStart.setClickable(true);

    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        progress = 0;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        vProgressBar.setProgress(values[0]);
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        while (progress < 100) {

            progress++;
            publishProgress(progress);
            SystemClock.sleep(100);

        }
        return null;

      }

    }

  }

In onProgressUpdate , use this to add the value to the current progress: onProgressUpdate ,使用onProgressUpdate值添加到当前进度中:

vProgressBar.setProgress(vProgressBar.getProgress() + values[0]);

And in doInBackground where you presumably have your "item", something like: doInBackground ,您大概拥有“项目”,例如:

publishProgress(item.getPercentageValue());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM