[文章]#HarmonyOS征文# HarmonyOS实战—统计按钮点击次数

阅读量0
0
1
统计10秒点击的次数
  • 在一定的时间内点击按钮,点击按钮的次数就会记录到 Text 文本中
  • 案例实现:
  • 新建项目:StatisticsApplication

ability_main
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4.     ohos:height="match_parent"
  5.     ohos:width="match_parent"
  6.     ohos:alignment="center"
  7.     ohos:orientation="vertical">

  8.     <Text
  9.         ohos:id="$+id:text1"
  10.         ohos:height="match_content"
  11.         ohos:width="match_content"
  12.         ohos:background_element="$graphic:background_ability_main"
  13.         ohos:layout_alignment="horizontal_center"
  14.         ohos:text="$string:mainability_HelloWorld"
  15.         ohos:text_size="40vp"
  16.         />

  17.     <Button
  18.         ohos:id="$+id:but1"
  19.         ohos:height="match_content"
  20.         ohos:width="match_content"
  21.         ohos:text="开始"
  22.         ohos:text_size="100"
  23.         ohos:background_element="red"
  24.         >

  25.     </Button>
  26. </DirectionalLayout>
复制代码

MainAbilitySlice
  1. package com.xdr630.statisticsapplication.slice;

  2. import com.xdr630.statisticsapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Button;
  6. import ohos.agp.components.Component;
  7. import ohos.agp.components.Text;

  8. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

  9.     Text text1;
  10.     Button but1;

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

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

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

  20.     }

  21.     @Override
  22.     public void onActive() {
  23.         super.onActive();
  24.     }

  25.     @Override
  26.     public void onForeground(Intent intent) {
  27.         super.onForeground(intent);
  28.     }


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

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

  35.     @Override
  36.     public void onClick(Component component) {
  37.         //点一次,计数器就自增一次
  38.         count++;
  39.         //统计10s之类,按了多少次,并把次数展示在文本框
  40.         if (flag){
  41.             //如果当前是第一次点击按钮,记录当前的时间
  42.             startTime = System.currentTimeMillis();
  43.             //当第一次点击之后游戏开始,修改按钮中的文字内容
  44.             but1.setText("请疯狂点我");
  45.             //修改标记,当第二次调用onClick方法时,flag为false,表示第二次点就不是第一次了,就会走else里的代码
  46.             flag = false;
  47.         }else{
  48.             if ((System.currentTimeMillis() - startTime) <= 10000){
  49.                 text1.setText(count + "");
  50.             }else {
  51.                 but1.setText("结束");
  52.                 //取消按钮点击事件,让该按钮不能被点击了
  53.                 but1.setClickable(false);
  54.             }
  55.         }
  56.     }
  57. }
复制代码

  • 运行:

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

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友