实现秒表

public String getMin() {
long min = (timeUsedInsec) / 60000;
return min < 10 ? "0" + min : min+"";
}

public String getSec() {
long sec = (timeUsedInsec / 1000) % 60;
return sec < 10 ? "0" + sec : sec+"";
}

public String getLongMill() {
long longmill = (timeUsedInsec/100) % 10;
return longmill + "";
}

public void addTimeUsed() {
timeUsedInsec += 100 ;
timeUsed = this.getMin() + ":" + this.getSec()+":"+this.getLongMill();

}



/**
* 更新时间的显示
*/
private void updateClockUI() {
minute.setText(getMin());
second.setText(getSec());
longmill.setText(getLongMill());
}




private void startTime() {
uiHandle.sendEmptyMessageDelayed(TICK_WHAT, TIME_TO_SEND);
}






private static final int TICK_WHAT = 2;
private static final int TIME_TO_SEND = 100;
private boolean isPaused = false; //判断是否暂停了
private boolean ifStart = false;// 判断是开始还是暂停,true为开始
private long timeUsedInsec; //总的时间,毫秒为单位,100毫秒递增

private String timeUsed; //总共用的时间,计次的时候需要用

private Handler uiHandle = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case TICK_WHAT:
if (!isPaused) { //是否暂停
addTimeUsed();//增加时间
updateClockUI(); //更新ui
}
uiHandle.sendEmptyMessageDelayed(TICK_WHAT, TIME_TO_SEND);//循环发送
break;
default:
break;
}

}
};




你可能感兴趣的:(实现秒表)