如果您在完成某些任务后尝试从单独的线程更改 TextView 的文本,那么您应该使用 Handler,否则您可能会遇到奇怪的错误。
EditText input;
Thread myWorkerThread;
AlertDialog alertDialog;
Handler successHandler = new Handler()
{
@Override
public void handleMessage(Message message)
{
setText("Success Message");
}
};
public void setText(String url)
{
input.setText("http://xxxx/" + url);
input.invalidate();
input.selectAll();
}
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
input = new EditText(this);
myWorkerThread = new Thread()
{
public void run()
{
// do your tasks here
// after task is done, call the handler
successHandler.sendEmptyMessage(0);
}
};
}
现在在启动 myWokerThread 之前显示您的警报对话框。
alertDialog = new AlertDialog.Builder(MyActivity.this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setView(input, 10, 0, 10, 0); // 10 spacing, left and right
alertDialog.show();
myWorkerThread.start();