您的位置:首页 > 编程语言

偷懒代码块之数字时钟效果

2015-11-25 14:30 302 查看
贴代码备忘~

public class MainActivity extends Activity {

private TextView tv;
private static long start,now;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView) findViewById(R.id.tvTime);
start = System.currentTimeMillis();
handle.post(runnable);
}

public String showTimeCount(long time) {
if(time >= 360000000){
return "00:00:00";
}
String timeCount = "";
long hourc = time/3600000;
String hour = "0" + hourc;
hour = hour.substring(hour.length()-2, hour.length());

long minuec = (time-hourc*3600000)/(60000);
String minue = "0" + minuec;
minue = minue.substring(minue.length()-2, minue.length());

long secc = (time-hourc*3600000-minuec*60000)/1000;
String sec = "0" + secc;
sec = sec.substring(sec.length()-2, sec.length());
timeCount = hour + ":" + minue + ":" + sec;
return timeCount;
}

Handler handle = new Handler();
Runnable runnable = new Runnable() {

@Override
public void run() {
handle.postDelayed(this, 500);
now = System.currentTimeMillis();
long time = now - start;
tv.setText(showTimeCount(time));
Log.i("0630","handle -- running");
}
};
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: