在我的计时器应用程序中,有一个进度条来设置倒数时间。我设法使进度条随着倒数的进度移动,但现在我要这样做:每当我启动倒数计时时,进度条的最大值应设置为计时器整数的值。所以例如如果我将倒计时设置为30秒并开始计时,则进度条应跳到右侧,并从右向左显示进度。类似于三星标准时钟应用倒计时上的圆圈。我该如何处理?我希望你明白我的意思。
public class MainActivity extends AppCompatActivity {
SeekBar timerSeekbar;
TextView timertextview;
boolean counteractiv = false;
Button gobutton;
CountDownTimer countDownTimer;
int progress;
public void resetTimer(){
countDownTimer.cancel();
timertextview.setText("0:30");
timerSeekbar.setEnabled(true);
counteractiv = false;
gobutton.setText("Start");
timerSeekbar.setMax(600);
timerSeekbar.setProgress(30);
}
public void buttonclicked(View view){
if(counteractiv){
resetTimer();
}else{
//timerSeekbar.setMax((int)secondsleft);
counteractiv = true;
timerSeekbar.setEnabled(false);
gobutton.setText("Stop!");
countDownTimer = new CountDownTimer(timerSeekbar.getProgress() * 1000, 1000){
@Override
public void onTick(long l) {
updatetimer((int)l/1000);
timerSeekbar.setProgress((int)l/1000);
}
@Override
public void onFinish() {
Toast.makeText(MainActivity.this, "Time over!", Toast.LENGTH_LONG).show();
gobutton.setText("Reset");
}
}.start();
timerSeekbar.setMax((int)progress/1000);
}
}
public void updatetimer(int secondsleft){
int minutes = secondsleft/60;
int seconds = secondsleft - (minutes*60);
String secondsString = Integer.toString(seconds);
String minutesString = Integer.toString(minutes);
if(seconds <=9){
secondsString = "0" + secondsString;
}
timertextview.setText(minutesString + ":" + secondsString);
timerSeekbar.setProgress(secondsleft);
progress = secondsleft;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerSeekbar = findViewById(R.id.seekBar);
timertextview = findViewById(R.id.CountdowntextView);
gobutton = findViewById(R.id.StartButton);
timerSeekbar.setMax(600);
timerSeekbar.setProgress(30);
timerSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
updatetimer(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
所以基本上我已经尝试将进度条转换为全局整数progress
,并使用它来定义timerSeekbar.setMax((int)progress)
但是我应该在哪里设置这一行代码以使其正常工作?
答案 0 :(得分:0)
尝试下面的代码,它可以工作。重置时,必须将setProgress设置为最大值。在这里,timertextview和seekbarmax应该匹配以获得正确的结果。另外,CountDownTimer构造函数应类似于以下示例。
public class TestActivity extends AppCompatActivity {
SeekBar timerSeekbar;
TextView timertextview;
boolean counteractiv = false;
Button gobutton;
CountDownTimer countDownTimer;
int progress;
public void resetTimer() {
countDownTimer.cancel();
timertextview.setText("0:30");
timerSeekbar.setEnabled(true);
counteractiv = false;
gobutton.setText("Start");
timerSeekbar.setMax(30);
timerSeekbar.setProgress(30);
}
public void buttonclicked(View view) {
if (counteractiv) {
resetTimer();
} else {
//timerSeekbar.setMax((int)secondsleft);
counteractiv = true;
// timerSeekbar.setEnabled(false);
gobutton.setText("Stop!");
countDownTimer = new CountDownTimer(30 * 1000, 1000) {
@Override
public void onTick(long l) {
updatetimer((int) l / 1000);
}
@Override
public void onFinish() {
Toast.makeText(EthopiaActivity.this, "Time over!", Toast.LENGTH_LONG).show();
gobutton.setText("Reset");
}
}.start();
}
}
public void updatetimer(int secondsleft) {
int minutes = secondsleft / 60;
int seconds = secondsleft - (minutes * 60);
String secondsString = Integer.toString(seconds);
String minutesString = Integer.toString(minutes);
if (seconds <= 9) {
secondsString = "0" + secondsString;
}
timertextview.setText(minutesString + ":" + secondsString);
timerSeekbar.setProgress(secondsleft);
progress = secondsleft;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerSeekbar = findViewById(R.id.seekBar);
timertextview = findViewById(R.id.CountdowntextView);
gobutton = findViewById(R.id.StartButton);
timerSeekbar.setMax(30);
timerSeekbar.setProgress(1);
timerSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
updatetimer(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
xml看起来像这样,在这里您必须删除静态值(最大值和进度)
<SeekBar
android:id="@+id/seekBar"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginStart="-5dp"
android:layout_marginTop="28dp" />
祝你好运。