2016-04-03 99 views
0

我有一個網格中有很多按鈕,是否可以禁用網格,這將導致所有的按鈕被禁用?Android禁用網格按鈕

之所以用後點擊一個按鈕回答問題,答案是檢查分數等計算出來的,但是如果我在發生這種情況時一直按下一個按鈕,它仍然會回答下一個問題等等。我使用一個處理程序來等待。禁用只是臨時性的,在一切都計算完畢後它們會再次啓用。

我該如何解決這個問題?而無需手動禁用每個按鈕。

在此先感謝。

編輯:

   GridLayout numgrid = (GridLayout) findViewById(R.id.numbersGrid); 
      GridLayout lettergrid = (GridLayout) findViewById(R.id.lettersGrid); 
      numgrid.setEnabled(false); 
      lettergrid.setEnabled(false); 

我想上面的代碼不能正常工作,因爲我需要它。

回答

1

顯然似乎沒有一種「簡單」的方法來解決這個問題。你可以做的是將佈局中的所有按鈕作爲一個數組,然後遍歷它們。類似下面的方法,禁止在GridLayout所有按鈕:

private void disableButtons(GridLayout layout) { 

    // Get all touchable views 
    ArrayList<View> layoutButtons = layout.getTouchables(); 

    // loop through them, if they are an instance of Button, disable it. 
    for(View v : layoutButtons){ 
     if(v instanceof Button) { 
      ((Button)v).setEnabled(false); 
     } 
    } 
} 

然後,只需通過在你的網格佈局:

GridLayout numGrid = (GridLayout) findViewById(R.id.numbersGrid); 
GridLayout letterGrid = (GridLayout) findViewById(R.id.lettersGrid); 

disableButtons(numGrid); 
disableButtons(letterGrid);