本人想动态更新TextView内容,但是没有反应。有没有相关代码。
解决方案
8
写个定时器。或写个线程。隔段时间去更新就哦了
8
新建个线程来更新textview里面的值,然后使用handler来更新界面!
8
class testThread extends Thread{ public void run() { Message message = new Message(); mHandler.sendMessage(message); } } //在handler中更新UI private Handler mHandler = new Handler(){ public void handleMessage(Message msg) { textView.setText("你想变的内容"); }; };
然后在你的代码中,执行这个线程
8
private final Timer timer = new Timer(); private TimerTask task; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub textView.setText("XXXX"); super.handleMessage(msg); } }; task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; timer.schedule(task, 1000, 1000);
或这样也行
8
mTestingButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//开始进行Open-Short的Testing
testDialog = ProgressDialog.show(InitMassage.this, “”, “Don”t touch the Screen,Open-Short Testing…”,true, false);
TimerTask task = new TimerTask(){
public void run(){
//execute the task
testDialog.cancel();
mShortTestNo.setText(” Pass”);
System.out.println(“1”);
}
};
Timer timer = new Timer();
timer.schedule(task, 6000);
System.out.println(“2”);
}
public void onClick(View v) {
//开始进行Open-Short的Testing
testDialog = ProgressDialog.show(InitMassage.this, “”, “Don”t touch the Screen,Open-Short Testing…”,true, false);
TimerTask task = new TimerTask(){
public void run(){
//execute the task
testDialog.cancel();
mShortTestNo.setText(” Pass”);
System.out.println(“1”);
}
};
Timer timer = new Timer();
timer.schedule(task, 6000);
System.out.println(“2”);
}
});
本人使用以上为什么不能在延迟6s后更改TextView的值为“Pass”,系统会自动崩溃!