• 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);
                }
            }
        }
    }
    
    • 运行:
      在这里插入图片描述

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

    在这里插入图片描述

    • 结束之后就不能再点击了
    • 也可以作进一步扩展,加个重置按钮点击事件,当结束后又可以点击重置按钮重新开始了,就不需要重新运行项目了
    • 【本文正在参与“有奖征文 | HarmonyOS征文大赛”活动】
      https://marketing.csdn.net/p/ad3879b53f4b8b31db27382b5fc65bbc

    本文来自博客园,作者:兮动人,转载请注明原文链接:https://www.cnblogs.com/xdr630/p/15254556.html

  • 相关阅读:
    Xcode8 1 创建coreData的ManagedObject后,报错 linker command failed with exit code 1
    在IOS中根据圆心坐标、半径和角度计算圆弧上的点坐标
    NodeJS学习目录
    nodeJS之URL
    nodeJS之域名DNS
    初识nodeJS
    使用nodeJS实现前端项目自动化之项目构建和文件合并
    nodeJS之fs文件系统
    nodeJS之二进制buffer对象
    nodeJS之进程process对象
  • 原文地址:https://www.cnblogs.com/xdr630/p/15254556.html
Copyright © 2020-2023  润新知