HarmonyOS实战—统计按钮点击次数

举报
兮动人 发表于 2021/08/06 23:11:09 2021/08/06
【摘要】 HarmonyOS实战—统计按钮点击次数

统计10秒点击的次数

  • 在一定的时间内点击按钮,点击按钮的次数就会记录到 Text 文本中
    在这里插入图片描述

  • 案例实现:

  • 新建项目:StatisticsApplication

ability_main

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

    <Text
        ohos:id="$+id:text1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:mainability_HelloWorld"
        ohos:text_size="40vp"
        />

    <Button
        ohos:id="$+id:but1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="开始"
        ohos:text_size="100"
        ohos:background_element="red"
        >

    </Button>
</DirectionalLayout>

MainAbilitySlice

package com.xdr630.statisticsapplication.slice;

import com.xdr630.statisticsapplication.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;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    Text text1;
    Button but1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //找到组件
        text1 = (Text) findComponentById(ResourceTable.Id_text1);
        but1 = (Button) findComponentById(ResourceTable.Id_but1);

        //给按钮设置单击事件
        but1.setClickedListener(this);

    }

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

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


    //如果flag为true,表示当前按钮是第一次点击
    //如果flag为false,表示当前按钮不是第一次点击
    boolean flag = true;
    long startTime = 0;

    //用来记录点击了多少次
    int count = 0;

    @Override
    public void onClick(Component component) {
        //点一次,计数器就自增一次
        count++;
        //统计10s之类,按了多少次,并把次数展示在文本框
        if (flag){
            //如果当前是第一次点击按钮,记录当前的时间
            startTime = System.currentTimeMillis();
            //当第一次点击之后游戏开始,修改按钮中的文字内容
            but1.setText("请疯狂点我");
            //修改标记,当第二次调用onClick方法时,flag为false,表示第二次点就不是第一次了,就会走else里的代码
            flag = false;
        }else{
            if ((System.currentTimeMillis() - startTime) <= 10000){
                text1.setText(count + "");
            }else {
                but1.setText("结束");
                //取消按钮点击事件,让该按钮不能被点击了
                but1.setClickable(false);
            }
        }
    }
}
  • 运行:
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 结束之后就不能再点击了
  • 也可以作进一步扩展,加个重置按钮点击事件,当结束后又可以点击重置按钮重新开始了,就不需要重新运行项目了
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200