android获取时间毫秒数,这样循环显示时间,怎么界面不显示啊??后台的Log输出正确
packagecom.example.time_1;importandroid.app.Activity;importandroid.os.Bundle;importan...
package com.example.time_1;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (TextView) findViewById(R.id.time);
while (true) {
long mill = System.currentTimeMillis();///// 当前时间对应的毫秒数
if (mill % 10000 == 0) {
Log.i("milltime", "当前时间:" + mill);
time.setText("当前时间:" + mill);
}
}
}
} 展开
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (TextView) findViewById(R.id.time);
while (true) {
long mill = System.currentTimeMillis();///// 当前时间对应的毫秒数
if (mill % 10000 == 0) {
Log.i("milltime", "当前时间:" + mill);
time.setText("当前时间:" + mill);
}
}
}
} 展开
1个回答
展开全部
你这样直接修改主页面,像你这样比较消耗时间的操作的话,系统一般来说是不会响应的,你应该放到线程里面执行这样比较耗时的操作
你可以这样写:
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;
}
}
};
}
你可以这样写:
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;
}
}
};
}
追问
你的正确。
switch (msg.what) {
case msgKey1:....我搞不明白,如果有多个msg,该如何处理呢?有实例代码吗?一个msg用不用switch()都行吧?
追答
是的,一个当然无所谓,可是我们写代码不是能跑就行的,写代码是为要考虑健壮性和可扩展性的,减少代码之间的耦合度是最基本的。所以刚开始就应该考虑好框架。能分开的尽量分开,不要怕麻烦,要不然后面会很难写...
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询