我想运行应用程序十秒钟,并从开始开始进度。
的值应该是什么progressBar.setProgress(100);
myCountDownTimer = new MyCountDownTimer(10000, 1000);
和
int progress = (int) (millisUntilFinished/100);
以下是我运行的代码:
package com.example.androidcountdownprogressbar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button buttonStart;
ProgressBar progressBar;
TextView textCounter;
MyCountDownTimer myCountDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button)findViewById(R.id.start);
progressBar = (ProgressBar)findViewById(R.id.progressbar);
textCounter = (TextView)findViewById(R.id.counter);
buttonStart.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
progressBar.setProgress(100);
myCountDownTimer = new MyCountDownTimer(10000, 1000);
myCountDownTimer.start();
}});
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
textCounter.setText(String.valueOf(millisUntilFinished));
int progress = (int) (millisUntilFinished/100);
progressBar.setProgress(progress);
}
@Override
public void onFinish() {
textCounter.setText("Finished");
progressBar.setProgress(0);
}
}
}