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