【发布时间】:2015-09-17 08:48:15
【问题描述】:
我在创建一个新类来计算时间时遇到问题。这是我的代码:
Button btcheck = (Button)findViewById(R.id.btcheck);
btcheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Time aa = new Time();
aa.RunTime();
}
});
和
public class Time extends MainActivity {
public void RunTime() {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
public Runnable updateTimerThread = new Runnable() {
public void run() {
updatedTime = SystemClock.uptimeMillis() - startTime;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
stime="" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds);
txttime.setText(stime + " ");
customHandler.postDelayed(this, 0);
};
}
但是它并没有像我预期的那样工作。希望你能帮助我。
【问题讨论】:
-
它没有在 txttime 中显示时间。
-
伙计,在你的最后一个问题中我提到过,如果一个类只是一个普通类,那么不要用 Activity 扩展它,因为创建一个扩展 Activity 的类的实例是一个非常繁重的问,而不是用一个活动来扩展它,如果你必须使用你的活动类实例,那么制作你的活动类的静态实例并使用它,就像我在你之前的问题中提到的那样。
-
非常感谢。我在stackoverflow.com/questions/32676002/… 有另一个问题。我希望你能再次帮助我。