用xLang写Timer事件
提示:xlang 是一款轻量级程序设计语言。基于QT 环境。配合 xStudio 进行代码编写。了解详情请点击:http://xlang.link
今天实践了 Timer 事件的编写。
完整代码:
btnStartTimer.setOnClickListener(new onClickListener() {
void onClick(QXObject obj, bool checked)override {
if (isButtonUse == false) {
isButtonUse = true;
tim1.schedule(new TimerTask() {
void run() {
runOnUi(new Runnable() {
void run() {
i+=1;
lblShowTimer.setText(String.format("%d",i));
}
});
}
},10,-1);
}
}
});
由于实例中,需要在UI界面显示内容,所以推荐使用 runOnUi 事件,写法如下:
runOnUi(new Runnable(){
void run(){
...setText..
}
});
另外,在定时器事件中,按钮需要做二次点击的预防,因为如果此时按钮处于Timer计时过程中,再去点击的话。会报错。这里我采用了 isButtonUsed 的布尔值去做标记和判断。