2021-01-26鸿蒙开发(七)

计时器demo
通过UI线程更新,实现计时器功能

java文件
package com.jinyou.basetest;

import com.jinyou.basetest.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import java.util.Timer;
import java.util.TimerTask;

public class MainAbility extends Ability {
    //计时timer
    private Timer timer;
    //计时
    private int count = 0;
    //状态位
    private boolean flag = true;
    //开始按键,回0按键
    private Button btn_zan, btn_o;
    //时间文本
    private Text text;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_main);
        //控件初始化
        btn_zan = (Button) findComponentById(ResourceTable.Id_btn1);
        btn_o = (Button) findComponentById(ResourceTable.Id_btn2);
        text = (Text) findComponentById(ResourceTable.Id_txt1);
        btn_zan.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            createTimer();
                            btn_zan.setText("暂停");
                        } else {
                            timer.cancel();
                            timer = null;
                            btn_zan.setText("继续");
                        }
                        flag = !flag;
                    }
                });


            }
        });
        btn_o.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (timer != null)
                    timer.cancel();
                timer = null;
                count = 0;
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        text.setText("" + count);
                        btn_zan.setText("开始");
                    }
                });

            }
        });
    }
  //开始计时
    private void createTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                count++;
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        text.setText("" + count);
                    }
                });
            }
        }, 0, 1000);

    }

}

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:orientation="vertical">

    <Text
        ohos:id="$+id:txt1"
        ohos:height="150vp"
        ohos:width="match_parent"
        ohos:left_margin="80vp"
        ohos:text_size="50fp"/>

    <Button
        ohos:id="$+id:btn1"
        ohos:height="60vp"
        ohos:width="match_parent"
        ohos:background_element="$graphic:background_ability_main"
        ohos:text="开始"
        ohos:text_size="50vp"
        />

    <Button
        ohos:id="$+id:btn2"
        ohos:height="60vp"
        ohos:width="match_parent"
        ohos:background_element="$graphic:background_ability_main"
        ohos:text="回0"
        ohos:text_size="50vp"
        ohos:top_margin="30vp"
        />
</DirectionalLayout>

运行效果
1


image.png

2


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 夜莺2517阅读 127,830评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 11,866评论 1 6
  • 我是一名过去式的高三狗,很可悲,在这三年里我没有恋爱,看着同龄的小伙伴们一对儿一对儿的,我的心不好受。怎么说呢,高...
    小娘纸阅读 8,779评论 4 7
  • 这些日子就像是一天一天在倒计时 一想到他走了 心里就是说不出的滋味 从几个月前认识他开始 就意识到终究会发生的 只...
    栗子a阅读 5,556评论 1 3

友情链接更多精彩内容