Java 设置一个Android按钮在一段时间后可见?
我有一个按钮,我不想被点击,直到一定的时间已经运行(比如说,5秒?),我试图创建一个这样的线程Java 设置一个Android按钮在一段时间后可见?,java,android,button,Java,Android,Button,我有一个按钮,我不想被点击,直到一定的时间已经运行(比如说,5秒?),我试图创建一个这样的线程 continueButtonThread = new Thread() { @Override public void run() { try { synchronized(this){ wait(5000);
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:错误/AndroidRuntime(14400):android.view.ViewRoot$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图
是否有其他方法可以绕过此问题?您可以使用
视图
类中的方法(一个按钮
是视图
的子级)您可以使用视图
类中的方法(一个按钮
是视图
的子级)您必须从UI线程更新视图。您所做的是从非ui线程进行更新
使用
或者,当您认为是更新视图可见性的正确时间时,使用handler和signalizehand.sendMessage(msg)
Handler hand = new Handler()
{
@Override
public void handleMessage(Message msg) {
/// here change the visibility
super.handleMessage(msg);
}
};
您必须从UI线程更新视图。您所做的是从非ui线程进行更新 使用 或者,当您认为是更新视图可见性的正确时间时,使用handler和signalize
hand.sendMessage(msg)
Handler hand = new Handler()
{
@Override
public void handleMessage(Message msg) {
/// here change the visibility
super.handleMessage(msg);
}
};
问题是,您只能在UI线程中触摸活动的视图。您可以使用
runOnUiThread
函数来执行此操作。我想建议你使用
handler.postDelayed(runnable, 5000)`
问题是,您只能在UI线程中触摸活动的视图。您可以使用
runOnUiThread
函数来执行此操作。我想建议你使用
handler.postDelayed(runnable, 5000)`
这是我找到的简单答案
Button button = (Button)findViewBYId(R.id.button);
button .setVisibility(View.INVISIBLE);
button .postDelayed(new Runnable() {
public void run() {
button .setVisibility(View.VISIBLE);
}
}, 7000);
这是我找到的简单答案
Button button = (Button)findViewBYId(R.id.button);
button .setVisibility(View.INVISIBLE);
button .postDelayed(new Runnable() {
public void run() {
button .setVisibility(View.VISIBLE);
}
}, 7000);