点击按钮后,我正尝试这样做:
case R.id.bcheckConnection:
if (IPok()) {
PlcState.ErrPlc = false;
Constant.adressIpPlc = adresIp.getText().toString();
final ProgressDialog dialog = ProgressDialog.show(Main.this, "", "Trying to connect...");
new Thread(new Runnable() {
public void run() {
timeout = network.testConnection(Constant.adressIpPlc, 102, 20000);
dialog.dismiss();
}
}).start();
if (timeout > -1) {
PlcState.ErrPlc = false;
stanPolaczenia.setText("Connection established. Timeout = ");
stanTimeout.setText(Long.toString(timeout));
currentIp.setText(Constant.adressIpPlc);
} else {
PlcState.ErrPlc = true;
stanPolaczenia.setText("Error");
stanTimeout.setText("");
currentIp.setText(Constant.adressIpPlc);
}
} else {
Toast.makeText(Main.this, "Wrong IP", Toast.LENGTH_LONG).show();
}
break;
因此可以在线程停止运行后更改文本吗?
答案 0 :(得分:1)
您可以使用Thread.join()
来阻止当前线程,直到给定的线程完成:
Thread myThread = new Thread(new Runnable() {
public void run() {
// Do something
}
});
myThread.start();
// Do some things
// and block current thread until myThread is finished
myThread.join();
// Continue execution after myThread got finished
修改:正如@Eric已在问题评论中提及:对于您的(示例)情况,使用AsyncTask
似乎更有意义。它有两个事件(在UI线程上调用),因此您可以使用进度更新和任务完成时更新UI。有关AsyncTask
示例,请参阅:AsyncTask Android example