用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
运行程序后如下
我们点击开始计时,表中数字部分开始运行
点击停止计时后,表中数字部分停止计时
标签:功能,void,计时,ohos,Override,import,秒表,public From: https://blog.51cto.com/u_15916339/5950813