2016-04-26 46 views
0
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() { 
    public void onClick(View v) { 
     ImageButton btn = (ImageButton) findViewById(R.id.imageButton2); 
     btn.setVisibility(View.GONE); 
    } 
}; 

我希望imageButton在點擊圖像視圖後消失或刪除。我在Android Studios上製作這個應用程序,當它運行時,我點擊imageButton視圖不會隱藏。點擊後如何移除imageButton?

謝謝所有願意幫助的人!

+1

參見[這個答案](http://stackoverflow.com/questions/7495088/how-do-i-make-a-button-invisible-只需點擊後) – GAVD

+0

按鈕 –

+1

的OnClickListener()中的setVisibility(View.Invisible)設置onClickListner,如Pablo在他的回答中所述。你會得到它的工作。 –

回答

3

你正在創建onClick處理常式的,但也許你不設置它:

ImageButton btn = (ImageButton) findViewById(R.id.imageButton2); 

View.OnClickListener imgButtonHandler3 = new View.OnClickListener() { 
    public void onClick(View v) { 
     btn.setVisibility(View.GONE); 
    } 
}; 

btn.setOnClickListener(imgButtonHandler3); // this sets the handler 
+0

謝謝帕布羅,我現在就試一試。對此,我真的非常感激! – Mason

0

我們可以在Android的禁用按鈕。嘗試下面的代碼。使用

View.OnClickListener imgButtonHandler3 = new View.OnClickListener() { 

    public void onClick(View v) { 
     ImageButton btn = (ImageButton) findViewById(R.id.imageButton2); 
     btn.setEnabled(false); 
    } 

}; 
+0

這是關於**刪除** ImageButton而不是禁用**它。 –

+0

然後這應該工作。 btn.setVisibility(View.GONE); – 2016-04-26 04:07:49

+3

爲什麼你發佈兩個不同的答案?編輯第一個答案或刪除一個 – droidev

-1

btn.setVisibility(View.INVISIBLE)代替View.Gone

View.OnClickListener imgButtonHandler3 = new View.OnClickListener() { 
    public void onClick(View v) { 
     ImageButton btn = (ImageButton) findViewById(R.id.imageButton2); 
     btn.setVisibility(View.INVISIBLE); 
    } 
}; 
+1

爲什麼這樣?它有什麼區別嗎?我的意思是關於如何消除你做了GONE或INVISIBLE的事情。兩者都將被刪除。 –

+0

否如果你正在做GONE,那麼它會從父視圖中移除視圖,並且該視圖的空間也會被移除,但是如果你正在做INVISIBLE,那麼空間將保持該視圖,而視圖是INVISIBLE。 – Pitty