13. 定时器组件练习

统计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="#FFF"
        ohos:background_element="#00F"
        ohos:layout_alignment="center"
        ohos:text_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="#F00"
        />


</DirectionalLayout>

MainAbilitySlice.java

package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

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

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. 给定时器一些基本设置
        //false:正向计时,true:反向计时
        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);
    }



    //是否第一次被点击
    //true:表示第一次被点击
    //false:表示不是第一次点击
    boolean first = true;

    //定义变量用来统计点击的次数
    int count = 0;

    //记录开始时间
    long startTime = 0;

    @Override
    public void onClick(Component component) {
        //当该方法被调用,证明按钮被点击了一次
        count++;

        //判断当前是否第一次点击
        if (first){
            //获取定时器中现在的时间 0:01
            //ticktimer.getText();//年 月 日 00:01
            startTime = String2long(ticktimer.getText());

            //修改按钮里的文本内容
            but.setText("请疯狂点我");

            //修改标记
            first = false;

            //开启定时器
            ticktimer.start();
        }
        //如果不是第一次点击
        //就不需要做上面的事情,直接修改文本的内容就可以了
        text.setText(count + "次");
    }

    //当定时器开始计时当时候,就会不断调用onTickTimerUpdate方法
    //tickTimer表示计时器的对象
    @Override
    public void onTickTimerUpdate(TickTimer tickTimer) {
        //1. 获取当时定时器的时间,并把该时间变为毫秒值
        long nowTime = String2long(tickTimer.getText());

        //2. 判断nowTime 跟 startTime之间有没有超过十秒
        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 result = date.getTime();

        return result;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值