我见过很多类似的问题,但我找不到任何实际有效的代码,所以请修改这个例子,以便进度条顺利进行。
我已经看到了一些增加最大值并减少时间间隔的答案,但我认为它没有正常工作。还有其他答案使用动画师类并且工作正常,但后来我不知道如何更新数字。
package com.example.arturo_2.countdowntimertutorial;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.os.CountDownTimer;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private long timeCountInMilliSeconds = 1 * 60 * 1000;
private ProgressBar progressBarCircle;
private EditText editTextMinute;
private TextView textViewTime;
private Button buttonDecrease;
private Button buttonIncrease;
private Button buttonStop;
private CountDownTimer countDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// method call to initialize the views
initViews();
// method call to initialize the listeners
initListeners();
start();
}
/**
* method to initialize the views
*/
private void initViews() {
progressBarCircle = (ProgressBar) findViewById(R.id.progressBarCircle);
editTextMinute = (EditText) findViewById(R.id.editTextMinute);
textViewTime = (TextView) findViewById(R.id.textViewTime);
buttonDecrease = (Button) findViewById(R.id.buttonDecrease);
buttonIncrease = (Button) findViewById(R.id.buttonIncrease);
buttonStop = (Button) findViewById(R.id.fab_stop);
}
/**
* method to initialize the click listeners
*/
private void initListeners() {
buttonDecrease.setOnClickListener(this);
buttonIncrease.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
/**
* method to increase timer value
*/
private void increase() {
timeCountInMilliSeconds += 15 * 1000;
setProgressBarValues();
}
/**
* method to decrease timer value
*/
private void decrease() {
timeCountInMilliSeconds -= 15 * 1000;
setProgressBarValues();
}
/**
* method to cancel count down timer
*/
private void stop() {
countDownTimer.cancel();
}
/**
* implemented method to listen clicks
*
* @param view
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonDecrease:
decrease();
break;
case R.id.buttonIncrease:
increase();
break;
case R.id.fab_stop:
stop();
break;
}
}
/**
* method to initialize the values for count down timer
*/
private void setTimerValues() {
int time = 0;
if (!editTextMinute.getText().toString().isEmpty()) {
// fetching value from edit text and type cast to integer
time = Integer.parseInt(editTextMinute.getText().toString().trim());
}
// assigning values after converting to milliseconds
timeCountInMilliSeconds = time * 60 * 1000;
setProgressBarValues();
}
/**
* method to set circular progress bar values
*/
private void setProgressBarValues() {
progressBarCircle.setMax((int) timeCountInMilliSeconds / 1000);
progressBarCircle.setProgress((int) timeCountInMilliSeconds / 1000);
}
/**
* method to start count down timer
*/
private void start() {
setTimerValues();
startCountDownTimer();
}
/**
* method to start count down timer
*/
private void startCountDownTimer() {
countDownTimer = new CountDownTimer(timeCountInMilliSeconds, 1000) {
@Override
public void onTick(long millisUntilFinished) {
textViewTime.setText(hmsTimeFormatter(millisUntilFinished));
progressBarCircle.setProgress((int) (millisUntilFinished / 1000));
}
@Override
public void onFinish() {
textViewTime.setText(hmsTimeFormatter(timeCountInMilliSeconds));
setProgressBarValues(); // call to initialize the progress bar values
}
}.start();
countDownTimer.start();
}
/**
* method to convert millisecond to time format
*
* @param milliSeconds
* @return mm:ss time formatted string
*/
private String hmsTimeFormatter(long milliSeconds) {
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(milliSeconds),
TimeUnit.MILLISECONDS.toSeconds(milliSeconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliSeconds)));
return hms;
}
}
答案 0 :(得分:0)
MainActivity.java,activity_main.xml
private void setProgressBarValues() {
progressBarCircle.setMax((int) timeCountInMilliSeconds / 50);
progressBarCircle.setProgress((int) timeCountInMilliSeconds / 1000);
}
private void startCountDownTimer() {
countDownTimer = new CountDownTimer(timeCountInMilliSeconds, 50) {
@Override
public void onTick(long millisUntilFinished) {
textViewTime.setText(hmsTimeFormatter(millisUntilFinished));
progressBarCircle.setProgress((int) (millisUntilFinished / 50));
}
@Override
public void onFinish() {
textViewTime.setText(hmsTimeFormatter(timeCountInMilliSeconds));
setProgressBarValues(); // call to initialize the progress bar values
}
}.start();
countDownTimer.start();
}