当前位置:   article > 正文

【鸿蒙】 使用定时器做一个简单的抢红包小游戏_鸿蒙5秒倒计时

鸿蒙5秒倒计时

1.新建项目

image-20220609213224034

image-20220609213327195

2. 添加 计时器,按钮组件

image-20220609213409009

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">

    <TickTimer
        ohos:id="$+id:tick_1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_color="red"
        ohos:text_size="50vp"
        ohos:text_alignment="center"
        ohos:layout_alignment="center"
        />

    <Button
        ohos:id="$+id:bt_1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:margin="30vp"
        ohos:clickable="false"
        ohos:text="准备!"
        ohos:text_color="red"
        ohos:text_size="50vp"
        ohos:text_alignment="center"
        ohos:layout_alignment="center"/>

</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

3.抢红包业务逻辑

image-20220609221630496

package com.sgg.hongbao.slice;

import com.sgg.hongbao.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TickTimer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.SimpleFormatter;

public class MainAbilitySlice extends AbilitySlice {

    Long money = 0L;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取定时器组件
        TickTimer tickTimer = (TickTimer) findComponentById(ResourceTable.Id_tick_1);
        //获取按钮组件
        Button bt = (Button) findComponentById(ResourceTable.Id_bt_1);


        tickTimer.setCountDown(false);

        tickTimer.start();

        // 10S 准备时间
        int countDwonTime = 3;

        tickTimer.setTickListener(tickTimer1 -> {
            Long aLong = string2Long(tickTimer1.getText());
            Long time = countDwonTime - aLong;

            if (aLong >= 10) {
                bt.setText(" 恭喜你 抢到 " + money + " 元 ");
                bt.setMultipleLine(true);
                //关闭定时器
                tickTimer.setText(" 00 : 00 ");
                tickTimer.stop();
                return;
            }

            if (time <= 0) {
                bt.setText("点我疯狂抢红包");
            } else {

                if (aLong == 0) {

                } else {
                    bt.setText(" 倒计时 " + time + "  秒");
                }
            }
        });


        bt.setClickedListener(component -> {
            money+=1000;
        });

    }


    private Long string2Long(String str) {

        long time = 0;
        try {
            time = new SimpleDateFormat("mm:ss").parse(str).getSeconds();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

4.效果演示

devecostudio64_w9yRtsBhCq

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/314321
推荐阅读
相关标签
  

闽ICP备14008679号