Android Studio 中使用 CountDownTimer 的定时器
Posted
技术标签:
【中文标题】Android Studio 中使用 CountDownTimer 的定时器【英文标题】:Timers in Android Studio using CountDownTimer 【发布时间】:2021-09-21 11:31:43 【问题描述】:请参阅我正在尝试获取用户输入的时间并使用用户输入尝试在 android Studio 4.2.2 中运行 CountDownTimer 函数
问题是:-
案例-1
当我通过变量名称代替 countDown 计时器函数的 millisInFuture 属性并尝试相应地设置 textview 时,TextView 没有设置任何内容。。 p>
public void timer()
CountDownTimer countDownTimer = new CountDownTimer(**timeValueIntent**, 1000)
@Override
public void onTick(long millisUntilFinished)
// mediaPlayer.setLooping(true);
//mediaPlayer.start();
timerValueTextView.setText(String.valueOf(millisUntilFinished / 1000) + "s");
@Override
public void onFinish()
Toast.makeText(getApplicationContext(), "Timed out", Toast.LENGTH_SHORT).show();
restartButton.setVisibility(View.VISIBLE);
restartButton.setEnabled(true);
quitButton.setVisibility(View.VISIBLE);
setBtnCond(true);
//mediaPlayer.stop();
.start();
`
案例-2
但是当我在 CountDownTimer 的 millisInFuture 属性中传递 1000 等 int 值并设置 textView 时,设置成功。
public void timer()
CountDownTimer countDownTimer = new CountDownTimer(**300000**, 1000)
@Override
public void onTick(long millisUntilFinished)
// mediaPlayer.setLooping(true);
//mediaPlayer.start();
timerValueTextView.setText(String.valueOf(millisUntilFinished / 1000) + "s");
@Override
public void onFinish()
Toast.makeText(getApplicationContext(), "Timed out", Toast.LENGTH_SHORT).show();
restartButton.setVisibility(View.VISIBLE);
restartButton.setEnabled(true);
quitButton.setVisibility(View.VISIBLE);
setBtnCond(true);
//mediaPlayer.stop();
.start();
`
谁能请帮我在 CountDownTimer 函数的 MillisInFuture 属性中传递变量名而不是硬编码的整数值。!!? `
【问题讨论】:
【参考方案1】:我有一些类似于你的问题的源代码,只是实现你的项目。
初始化变量
private static final long START_TIME_IN_MILLIS = 600000;
private TextView mTextViewCountDown;
private Button mButtonStartPause;
private Button mButtonReset;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
onCreate
//Init
mTextViewCountDown = findViewById(R.id.text_view_countdown);
mButtonStartPause = findViewById(R.id.button_start_pause);
mButtonReset = findViewById(R.id.button_reset);
mButtonStartPause.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (mTimerRunning)
pauseTimer(); //pauseFunction
else
startTimer(); //StartFunction
);
mButtonReset.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
resetTimer(); // resetFunction
);
updateCountDownText(); //updateEverySecond
startTimer()
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000)
@Override
public void onTick(long millisUntilFinished)
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
@Override
public void onFinish()
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonStartPause.setVisibility(View.INVISIBLE);
mButtonReset.setVisibility(View.VISIBLE);
.start();
mTimerRunning = true;
mButtonStartPause.setText("pause");
mButtonReset.setVisibility(View.INVISIBLE);
pauseTimer()
mCountDownTimer.cancel();
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonReset.setVisibility(View.VISIBLE);
resetTimer()
mTimeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
mButtonReset.setVisibility(View.INVISIBLE);
mButtonStartPause.setVisibility(View.VISIBLE);
updateCountDownText()
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
最后是XML
<TextView
android:id="@+id/text_view_countdown"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="00:00"
android:textColor="@android:color/black"
android:textSize="60sp" />
<Button
android:id="@+id/button_start_pause"
android:layout_
android:layout_
android:layout_below="@+id/text_view_countdown"
android:layout_centerHorizontal="true"
android:text="start" />
<Button
android:id="@+id/button_reset"
android:layout_
android:layout_
android:layout_below="@+id/text_view_countdown"
android:layout_marginStart="11dp"
android:layout_toEndOf="@+id/button_start_pause"
android:text="reset"
android:visibility="invisible"
tools:visibility="visible" />
【讨论】:
欢迎 Tiwary。 嘿,如果你不介意我们可以通过任何其他社交平台联系,我是 Android 开发新手,也许我们可以更频繁地互相帮助。 @KHUSHALTiwary LinkedIn 个人资料中的 bio connect me以上是关于Android Studio 中使用 CountDownTimer 的定时器的主要内容,如果未能解决你的问题,请参考以下文章