【发布时间】:2013-06-14 16:34:24
【问题描述】:
我创建了继承自 AlertDialog 的类。此类启动线程,通过互联网检查一些值(操作时间长)。该对话框有一个按钮(POSITIVE_BUTTON)。在线程结束之前,应该禁用此按钮。但我不知道如何从线程中启用此按钮。
我的代码:
public MyDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.dialog, null);
setView(view);
getButton(BUTTON_POSITIVE).setEnabled(false);
new Thread(new Runnable() {
public void run() {
// Long operation time code
getButton(BUTTON_POSITIVE).setEnabled(true); // CRASH!!!
}
}).start();
}
错误表明我从另一个线程更改视图。而且我不能使用runOnUIThread 方法,因为它不会在AlertDialog 中退出。
我该怎么办?
【问题讨论】:
-
你能在你的构造函数中传递一个 Activity 吗?
标签: android multithreading dialog