当前位置:网站首页>[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



2. add to timer , Button components

<?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

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

边栏推荐
- ssh公钥登录失败报错:sign_and_send_pubkey: no mutual signature supported
- TCP three handshakes and four waves
- Final summary of addition, deletion, modification and query - 2.1 (single table - addition, deletion, modification and query)
- Laravel 8 selects JWT for interface verification
- 2020-12-06
- Demand and business model innovation - demand 11 - overview of demand analysis
- Audio and video technology under the trend of full true Internet | Q recommendation
- 2020-12-12
- The four pain points of enterprise digitalization are solved by low code platform
- Final summary of addition, deletion, modification and query - 2.2 (knowledge points in mapper.xml)
猜你喜欢

laravel 8 选用 jwt 进行接口验证

Demand and business model innovation - demand 8- interview

Comparaison de la taille des fractions

Wechat applet project example - renju for two

【高代码文件格式API】道宁为您提供文件格式API集——Aspose,只需几行代码即可创建转换和操作100多种文件格式

What is the core of Web3?

Recommend 6 office software, easy to use and free, double the efficiency

【点云压缩】Sparse Tensor-based Point Cloud Attribute Compression

ARD3M电动机保护器在煤炭行业中的应用

如何防止商场电气火灾的发生?
随机推荐
Audio and video technology under the trend of full true Internet | Q recommendation
How to prevent electrical fire in shopping malls?
cupp字典生成工具(同类工具还有crunch)
Red's deleted number
[digital signal processing] correlation function (energy signal | cross correlation function of energy signal | autocorrelation function of energy signal)
Selection (044) - what is the output of the following code?
Oracle sequence
Oracle users and tablespaces
Comment prévenir les incendies électriques dans les centres commerciaux?
Maya foreground rendering plug-in Mel scripting tool
I2C协议概述
ssh公钥登录失败报错:sign_and_send_pubkey: no mutual signature supported
2020-12-06
跨域有哪些解决方法?
分数大小的比较
Maya Front Office Rendering plug - in Mel script Tool
[digital signal processing] correlation function (finite signal | autocorrelation function of finite signal)
Selection (043) - which of the following values are false?
Wechat applet project example - renju for two
min25筛