统计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;
}
}