本文介绍了Android-在onClick操作中隐藏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这样的onClick动作中,我需要隐藏一个按钮:
I need to hide a button during an onClick action like this:
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
Button button2 = (Button) findViewById(R.id.button2);
button2.setVisibility(View.GONE);
//Some methods
//...
button2.setVisibility(View.VISIBLE);
break;
}
但是可见性仅在onClick之后改变,我该怎么做才能在onstrong中隐藏按钮 ?
But the visibility changes only after the onClick, what could I do to hide the button during the onClick?
谢谢
推荐答案
当然,因为您正在同一线程中执行所有操作,因此可能会发现可见性发生变化,请尝试以下操作:
of course because you are executing all operation in the same thread you may notice the visibility changement, try this :
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
final button2 = (Button) findViewById(R.id.button2);
button2.setVisibility(View.GONE);
setVisibility(GONE);
new Thread(new Runnable() {
@Override
public void run() {
//your work
runOnUiThread(new Runnable() { //resetting the visibility of the button
@Override
public void run() {
//manipulating UI components from outside of the UI Thread require a call to runOnUiThread
button2.setVisibility(VISIBLE);
}
});
}
}).start();
break;
}
}
这篇关于Android-在onClick操作中隐藏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!