Android计时器倒计时完成
创始人
2024-10-08 14:31:24
0

以下是一个使用Android的倒计时器完成的代码示例:

import android.os.CountDownTimer;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView countdownText;
    private CountDownTimer countDownTimer;
    private long timeLeftInMillis = 60000; // 设定倒计时的总时间,这里为60秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        countdownText = findViewById(R.id.countdown_text);

        countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftInMillis = millisUntilFinished;
                updateCountdownText();
            }

            @Override
            public void onFinish() {
                countdownText.setText("倒计时完成");
            }
        }.start();
    }

    private void updateCountdownText() {
        int minutes = (int) (timeLeftInMillis / 1000) / 60;
        int seconds = (int) (timeLeftInMillis / 1000) % 60;

        String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

        countdownText.setText(timeLeftFormatted);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        countDownTimer.cancel();
    }
}

这个示例中,我们使用了Android的CountDownTimer类来实现倒计时功能。在onCreate方法中,我们初始化了CountDownTimer,并设定了倒计时的总时间和每次倒计时的间隔时间。在onTick方法中,我们更新了剩余时间并更新了倒计时文本。在onFinish方法中,我们在倒计时结束时显示"倒计时完成"的文本。

请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。

相关内容

热门资讯

透视脚本!aapoker脚本怎... 透视脚本!aapoker脚本怎么用(透视)一贯是有挂(详细辅助规律教程)1、超多福利:超高返利,海量...
透视真的!wepokerh5破... 透视真的!wepokerh5破解(透视)本来有挂(详细辅助系统教程)1、构建自己的辅助插件;2、选择...
透视透视!wepoker有插件... 透视透视!wepoker有插件吗(透视)好像有挂(详细辅助介绍教程)1、玩家可以在软件透明挂俱乐部对...
透视肯定!有没有人wepoke... 透视肯定!有没有人wepoker(透视)好像真的有挂(详细辅助2025新版教程)辅助器中分为三种模型...
透视攻略!wepokerplu... 透视攻略!wepokerplus透视脚本免费(透视)确实真的有挂(详细辅助必胜教程);透视攻略!we...
透视软件!wepoker模拟器... 透视软件!wepoker模拟器哪个好用(透视)总是有挂(详细辅助攻略教程)辅助器中分为三种模型:软件...
透视app!wepoker免费... 透视app!wepoker免费透视(透视)一直有挂(详细辅助科技教程)一、软件透明挂的定义与意义1、...
透视最新!wepoker安装教... 透视最新!wepoker安装教程(透视)好像真的有挂(详细辅助实用技巧);1、下载好辅助软件之后点击...
透视有挂!wepoker私人局... 透视有挂!wepoker私人局辅助器怎么用(透视)好像存在有挂(详细辅助2025新版技巧)一、软件透...
透视科技!菠萝辅助器免费版的特... 透视科技!菠萝辅助器免费版的特点(透视)确实有挂(详细辅助曝光教程)1、透视辅助简单,软件透明挂操作...