HarmonyOS开发27:TickTimer定时器组件练习

统计10秒之内按了多少次?

ability_main.xml代码:

<?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:ticktimer"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_size="30fp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="#FF43C3DE"
        ohos:text_alignment="center"
        ohos:layout_alignment="center"
        />
    <Text
        ohos:id="$+id:count"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:top_margin="10vp"
        ohos:text="0次"
        ohos:text_size="30fp"
        />
    <Button
        ohos:id="$+id:but"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:top_margin="10vp"
        ohos:text_size="30fp"
        ohos:text="开始计时"
        ohos:background_element="#FFDE7332"
        />

</DirectionalLayout>

MainAbilitySlice.java代码:

package com.example.ticktimerapplication.slice;

import com.example.ticktimerapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TickTimer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener, TickTimer.TickListener {
    TickTimer tickTimer;
    Text text;
    Button but;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1.找到组件
        tickTimer = (TickTimer) findComponentById(ResourceTable.Id_ticktimer);
        text = (Text) findComponentById(ResourceTable.Id_count);
        but = (Button) findComponentById(ResourceTable.Id_but);

        //2.给按钮绑定单击事件
        but.setClickedListener(this);

        //3.给定时器设置基本设置
        //正向计时
        tickTimer.setCountDown(false);
        //计时格式
        tickTimer.setFormat("mm:ss");

        //4.给定时器绑定定时事件
        tickTimer.setTickListener(this);

    }

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

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


    //记录是否为第一次点击
    boolean first = true;
    //记录点击次数
    int count = 0;
    //记录开始时间
    long startTime = 0;
    @Override
    public void onClick(Component component) {
        //当调用该方法证明点击了按钮一次
        count++;
        //判断是否是首次点击
        if(first){
            //获取点击时间
            //ticktimer.getText();//"比如00:03"
            startTime = String2Long(tickTimer.getText());
            but.setText("请疯狂点我");
            first = false;
            tickTimer.start();
        }
        text.setText(count + "次");
    }

    //当定时器开始计时时,会不断调用onTickTimerUpdate方法
    //tickTimer表示计时器对象
    @Override
    public void onTickTimerUpdate(TickTimer tickTimer) {
        //获取定时器当前的时间
        long nowTime = String2Long(tickTimer.getText());
        if(nowTime-startTime >= 10000){
            tickTimer.stop();
            text.setText("最终成绩为: "+ count + "次");
            but.setText("游戏结束了");
            but.setClickable(false);
        }
    }

    //作用:把字符串时间变成毫秒值
    public long String2Long(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ret = date.getTime();
        return ret;
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GeniusAng丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值