当计时器也减少时,如何让搜索栏滑动 automatically/sync 到 CountDownTimer
How to let the seekbar slide automatically/sync to the CountDownTimer as the timer decreases too
我尝试结合使用 seekBar 和 CountDownTimer,其中我将 seekBar 滑动到 5 秒然后释放,然后它会命中 CountDownTimer 代码以从 5 秒计数到 0 秒。我的问题是一旦我释放 seekBar 幻灯片,我希望幻灯片也减少(向左移动)(自动)与计时器和幻灯片达到 0 同步。帮助我谢谢!!
package com.example.timerrr;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
CountDownTimer mCount;
SeekBar seekbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbar= (SeekBar) findViewById(R.id.seekBarTimer);
seekbar.setMax(60000);
seekbar.setProgress(0);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(final SeekBar seekBar, final int progress, boolean fromUser) {
Log.i("countdown to: ", Integer.toString(progress/1000));
mCount = new CountDownTimer(progress, 1000 ){
public void onTick(long x){
Log.i("Countdown",Long.toString(x/1000));
}
public void onFinish(){
Log.i("Countdown","Finish");
}
};
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mCount.start();
}
});
}
}
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginLeft="162dp"
android:layout_marginTop="428dp"
android:layout_marginEnd="162dp"
android:layout_marginRight="162dp"
android:onClick="startButton11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="@+id/seekBarTimer"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="57dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
试试这个:
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(final SeekBar seekBar, final int progress, boolean fromUser) {
Log.i("countdown to: ", Integer.toString(progress/1000));
if(!fromUser)
return;
mCount = new CountDownTimer(progress, 1000 ){
public void onTick(long x){
Log.i("Countdown",Long.toString(x/1000));
seekBar.setProgress((int) x);
}
public void onFinish(){
Log.i("Countdown","Finish");
}
};
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if(mCount != null)
mCount.cancel();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if(mCount != null)
mCount.start();
}
});
我尝试结合使用 seekBar 和 CountDownTimer,其中我将 seekBar 滑动到 5 秒然后释放,然后它会命中 CountDownTimer 代码以从 5 秒计数到 0 秒。我的问题是一旦我释放 seekBar 幻灯片,我希望幻灯片也减少(向左移动)(自动)与计时器和幻灯片达到 0 同步。帮助我谢谢!!
package com.example.timerrr;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
CountDownTimer mCount;
SeekBar seekbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbar= (SeekBar) findViewById(R.id.seekBarTimer);
seekbar.setMax(60000);
seekbar.setProgress(0);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(final SeekBar seekBar, final int progress, boolean fromUser) {
Log.i("countdown to: ", Integer.toString(progress/1000));
mCount = new CountDownTimer(progress, 1000 ){
public void onTick(long x){
Log.i("Countdown",Long.toString(x/1000));
}
public void onFinish(){
Log.i("Countdown","Finish");
}
};
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mCount.start();
}
});
}
}
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginLeft="162dp"
android:layout_marginTop="428dp"
android:layout_marginEnd="162dp"
android:layout_marginRight="162dp"
android:onClick="startButton11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="@+id/seekBarTimer"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="57dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
试试这个:
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(final SeekBar seekBar, final int progress, boolean fromUser) {
Log.i("countdown to: ", Integer.toString(progress/1000));
if(!fromUser)
return;
mCount = new CountDownTimer(progress, 1000 ){
public void onTick(long x){
Log.i("Countdown",Long.toString(x/1000));
seekBar.setProgress((int) x);
}
public void onFinish(){
Log.i("Countdown","Finish");
}
};
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if(mCount != null)
mCount.cancel();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if(mCount != null)
mCount.start();
}
});