设置 Android 按钮在一段时间后可见?
我有一个按钮,我不想在运行一定时间(例如 5 秒?)之前单击该按钮,我尝试创建一个像这样的线程
continueButtonThread = new Thread()
{
@Override
public void run()
{
try {
synchronized(this){
wait(5000);
}
}
catch(InterruptedException ex){
}
continueButton.setVisibility(0);
}
};
continueButtonThread.start();
但是我无法在不同的线程中修改按钮的 setVisibility 属性线。这是来自 LogCat 的错误:
10-02 14:35:05.908: ERROR/AndroidRuntime(14400): android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。
还有其他方法可以解决这个问题吗?
I have a button that I don't want to be clickable until a certain amount of time has run (say, 5 seconds?) I tried creating a thread like this
continueButtonThread = new Thread()
{
@Override
public void run()
{
try {
synchronized(this){
wait(5000);
}
}
catch(InterruptedException ex){
}
continueButton.setVisibility(0);
}
};
continueButtonThread.start();
But I can't modify the setVisibility property of the button within a different thread. This is the error from the LogCat:
10-02 14:35:05.908: ERROR/AndroidRuntime(14400): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Any other way to get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您只能在 UI 线程中触摸 Activity 的视图。您可以使用 runOnUiThread 函数来完成此操作。我想建议你使用
The problem is that you can touch views of your activity only in UI thread. you can do it by using
runOnUiThread
function. I would like to suggest you to use您必须从 UI 线程更新您的视图。您正在做的是从非 ui 线程进行更新。
使用或使用处理程序并发出信号
hand.sendMessage(msg)
当您认为更新视图可见性的正确时机时,
You must update your view from UI-thread. What you are doing is you are updating from non-ui-thread.
Use
or use handler and signalize
hand.sendMessage(msg)
when you think is right time to update the view visibility您可以使用 postDelayed<
View
类中的 /a> 方法(Button
是View
的子级)You can use the postDelayed method from the
View
class (AButton
is a child ofView
)这是我找到的简单答案
here is simple answer i found