Android开发,实现欢迎页/启动页倒计时

1. 涉及到的技术点

  1. CountDownTimer的使用
  2. CardView卡片控件的使用
  3. 如何将程序启动的第一个页面设置为启动页或欢迎页

2. 实现步骤

  1. 创建倒计时布局文件
  2. 使用 CountDownTimer实现倒计时
  3. 如何将程序第一个页面设置为启动页或欢迎页

  • 创建倒计时布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@mipmap/img_welcome"
    tools:context=".WelcomeActivity">


    <androidx.cardview.widget.CardView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_margin="20dp"
        android:backgroundTint="#20000000"
        android:elevation="0dp"
        app:cardCornerRadius="25dp">

        <TextView
            android:id="@+id/tv_countdown"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="3 s"
            android:textSize="16sp"
            android:textStyle="bold" />
    </androidx.cardview.widget.CardView>

</RelativeLayout>
  • 在WelcomeActivity中实现
public class WelcomeActivity extends AppCompatActivity {

    private TextView tvCountdown;
    private CountDownTimer countDownTimer;
    private long timeLeftInMillis = 3000; // 设置倒计时时长,单位为毫秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        //初始化控件
        tvCountdown = findViewById(R.id.tv_countdown);
        // 启动倒计时
        startCountdown();
    }

    private void startCountdown() {
        countDownTimer =new CountDownTimer(timeLeftInMillis,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftInMillis = millisUntilFinished;
                int secondsRemaining = (int) (millisUntilFinished / 1000);
                tvCountdown.setText(secondsRemaining +" s");
            }

            @Override
            public void onFinish() {
                // 倒计时结束后的操作,例如跳转到主页面
                finish();
                //然后跳转到登录页面(看自己逻辑想跳转哪个页面)
                startActivity(new Intent(WelcomeActivity.this, ProductActivity.class));

            }
        }.start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}

3. 运行效果图

在这里插入图片描述

4. 视频教程

  1. 教程地址: https://www.bilibili.com/video/BV1zw411676Z/?vd_source=984bb03f768809c7d33f20179343d8c8
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值