发布网友 发布时间:2022-04-20 10:34
共1个回答
热心网友 时间:2023-09-30 17:41
你这样直接修改主页面,像你这样比较消耗时间的操作的话,系统一般来说是不会响应的,你应该放到线程里面执行这样比较耗时的操作
你可以这样写:
public class TestActivity extends Activity {
private static final int msgKey1 = 1;
private TextView mTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
Time = (TextView) findViewById(R.id.time);
new TimeThread().start();
}
public class TimeThread extends Thread {
@Override
public void run () {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = msgKey1;
mHandler.sendMessage(msg);
}
catch (InterruptedException e) {
e.printStackTrace();
}
} while(true);
}
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage (Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case msgKey1:
long sysTime = System.currentTimeMillis();
CharSequence sysTimeStr = DateFormat.format("hh:mm:ss", sysTime);
Time.setText(sysTimeStr);
break;
default:
break;
}
}
};
}