首页 > 其他分享 >小秒表计时的功能实现

小秒表计时的功能实现

时间:2022-12-18 10:31:09浏览次数:29  
标签:功能 void 计时 ohos Override import 秒表 public

用DevEco工具

首先给main_layout.xml一个按钮一个文本

<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:background_element="#000000">
<TickTimer
ohos:id="$+id:tick_timer"
ohos:width="match_content"
ohos:height="match_content"
ohos:center_in_parent="true"
ohos:text="00:00:00"
ohos:text_color="#007DFF"
ohos:text_size="32fp"/>
<Button
ohos:id="$+id:button"
ohos:width="match_content"
ohos:height="match_content"
ohos:text_size="19fp"
ohos:text="开始计时"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:right_padding="80vp"
ohos:left_padding="80vp"
ohos:background_element="$graphic:button_element"
ohos:center_in_parent="true"
ohos:align_parent_bottom="true"/>
</DependentLayout>

button_element.xml用来显示按钮的背景

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="oval">
<solid
ohos:color="#007DFF"/>
</shape>

在实现功能之前先创建一个TimeFormat 的工具类

package com.example.myapplication3.slice;

public class TimeFormatUtil {
public static String toDisplayString(int timeHundreds){
int hundreds,seconds,minutes;

String[] formatterArrayMillis = new String[2];
String formattedSeconds,formattedMinutes;

hundreds=timeHundreds%100;
String milliSecStr = Integer.toString(hundreds);
formatterArrayMillis[0] = "0" + milliSecStr;
formatterArrayMillis[1] = milliSecStr;

seconds=(timeHundreds/=100)%60;
minutes=(timeHundreds/=60)%60;

//format output
formattedSeconds=Integer.toString(seconds/10)+
Integer.toString(seconds%10);
formattedMinutes=Integer.toString(minutes/10)+
Integer.toString(minutes%10);

int millSecDigitsCnt = milliSecStr.length();
String timeString =
formattedMinutes+":"+
formattedSeconds+"."+
formatterArrayMillis[millSecDigitsCnt - 1];

return timeString;
}
}

在mainability.java中加载Xml布局,并且 onStart()方法加入功能代码

package com.example.myapplication3;

import com.example.myapplication3.slice.MainAbilitySlice;
import com.example.myapplication3.slice.TimeFormatUtil;
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 ohos.app.dispatcher.TaskDispatcher;

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

public class MainAbility extends Ability {
private Timer mTimer;
private Boolean isButtonStartPressed = false;
@Override
public void onStart(Intent intent) {

super.onStart(intent);
super.setUIContent(ResourceTable.Layout_main_layout);
Button mButton = (Button) findComponentById(ResourceTable.Id_button);
Text mText = (Text) findComponentById(ResourceTable.Id_tick_timer);
TaskDispatcher uiTaskDispatcher = getUITaskDispatcher();

if (mButton != null){
// 为按钮设置点击回调
mButton.setClickedListener(new Component.ClickedListener()
{
@Override
public void onClick(Component component){

final int[] currentTime = {0};
if (isButtonStartPressed) {
System.out.println("停止计时!!!!!");
isButtonStartPressed = false;
uiTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
mButton.setText("开始计时");
}
});
mTimer.cancel();
}else {
System.out.println("开始计时!!!!!!!");
isButtonStartPressed = true;
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run(){
currentTime[0] += 1;
uiTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run(){
mText.setText(TimeFormatUtil.toDisplayString(currentTime[0]));
mButton.setText("停止计时");
}
});
}
}, 0, 10);
}
}
});
}
}
@Override
public void onActive() {
super.onActive();
}

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

选择Wearable

小秒表计时的功能实现_ide

运行程序后如下

小秒表计时的功能实现_xml_02

我们点击开始计时,表中数字部分开始运行

小秒表计时的功能实现_xml_03

点击停止计时后,表中数字部分停止计时

小秒表计时的功能实现_xml_04

标签:功能,void,计时,ohos,Override,import,秒表,public
From: https://blog.51cto.com/u_15916339/5950813

相关文章

  • 前端开发系列134-进阶篇之脚手架Yue-cli的实现03-download功能
    title:前端开发系列134-进阶篇之脚手架Yue-cli的实现03-download功能tags:categories:[]date:2019-11-0400:00:08这是系列文章前端脚手架实现的第三篇,本文核心解......
  • day04-功能实现03
    家居网购项目实现038.功能07-后台管理显示家居8.1需求分析/图解给后台管理提供独立登录页面,管理员登录地址不对外公开管理员登录成功后,显示管理菜单页面管理员点......
  • 双网口主机通过 docker 安装 openwrt 实现软路由功能
    在docker中安装openwrt,而不是在openwrt中安装docker,真正做到ALLINONE特点不用重新购买设备(只需要再买个usb网卡或者minipcie网卡等等都行)性能及其强......
  • 信号功能实战
    1. 信号简介    (1)每个线程都有自己的信号屏蔽字。    (2)每个线程都对同一信号都共享一个信号处理函数。    (3)当一个线程调用pthread_create()创......
  • pytest + yaml 框架 -17.文件上传功能
    前言本插件集成了requests_toolbelt插件处理Content-Type:multipart/form-data类型文件上传接口。文件上传multipart/form-data用fiddler抓包,查看抓到的接口,以下这......
  • conda安装配置jupyter notebook + 代码jupyter notebook自动补全功能
    步骤一:安装jupyternotebook  步骤二:找到jupyternotebook配置文件的位置 步骤三:打开配置文件jupyter_notebook_config.py,作出如下修改:找到 c.NotebookApp......
  • 前端开发系列106-小程序篇之衣橱商城首页功能实现
    title:'前端开发系列106-小程序篇之衣橱商城首页功能实现'tags:categories:[]date:2018-12-2118:30:39本文简单说明衣橱商城首页界面的实现过程,包括对商城首页的......
  • 功能测试用例的编写思路及流程
    功能测试用例的编写需要按照一定的思路进行,而不是想到哪写到哪,一般测试机制成熟的公司都会有公司自己自定义的测试用例模板,以及一整套的测试流程关注点,测试人员在测试......
  • day03-功能实现02
    家居网购项目实现025.功能04-会员登录5.1需求分析/图解需求如图:输入用户名、密码后提交判断该用户是否存在如果存在,显示登录成功页面否则返回登录页面,要求重新登......
  • 好用到爆,GitHub 星标 32.5k+的命令行软件管理神器,功能真强大
    前言(废话)本来打算在公司偷偷摸摸给星球的用户写一篇编程喵整合MongoDB的文章,结果在通过brew安装MongoDB的时候竟然报错了。原因很简单,公司这台Mac上的homebrew环......