当前位置:网站首页>[Hongmeng] use the timer to play a simple game of robbing red envelopes

[Hongmeng] use the timer to play a simple game of robbing red envelopes

2022-06-12 03:07:00 Autumn sunset

1. New projects

image-20220609213224034

image-20220609213327195

2. add to timer , Button components

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=" Get ready !"
        ohos:text_color="red"
        ohos:text_size="50vp"
        ohos:text_alignment="center"
        ohos:layout_alignment="center"/>

</DirectionalLayout>

3. Red envelope business logic

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);

        //  Get timer component 
        TickTimer tickTimer = (TickTimer) findComponentById(ResourceTable.Id_tick_1);
        // Get button component 
        Button bt = (Button) findComponentById(ResourceTable.Id_bt_1);


        tickTimer.setCountDown(false);

        tickTimer.start();

        // 10S  Time to prepare 
        int countDwonTime = 3;

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

            if (aLong >= 10) {
                bt.setText("  congratulations   Grab  " + money + "  element  ");
                bt.setMultipleLine(true);
                // off timer 
                tickTimer.setText(" 00 : 00 ");
                tickTimer.stop();
                return;
            }

            if (time <= 0) {
                bt.setText(" I am crazy to grab red envelopes ");
            } else {

                if (aLong == 0) {

                } else {
                    bt.setText("  count down  " + time + "   second ");
                }
            }
        });


        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);
    }
}

4. Effect demonstration

devecostudio64_w9yRtsBhCq

原网站

版权声明
本文为[Autumn sunset]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120302146108.html