簡體   English   中英

Android:批量啟用按鈕已被禁用

[英]Android: mass enable buttons which are already disabled

我已經嘗試使用下面鏈接中提到的代碼“質量禁用”按鈕,它工作得很好。 但是,相同的代碼不適用於批量啟用。

Android:批量啟用/禁用按鈕

禁用代碼(工作)

TableLayout tl = (TableLayout)findViewById(R.id.table1); // 
ArrayList<View> touchables = tl.getTouchables();
for(View touchable : touchables){
if( touchable instanceof Button && touchable != btnNewWord )
((Button)touchable).setEnabled(false);
}

啟用代碼(不工作)

btnNewWord.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

TableLayout tl = (TableLayout)findViewById(R.id.table1);  
ArrayList<View> touchables = tl.getTouchables();
for(View touchable : touchables){
if( touchable != btnNewWord )
((Button)touchable).setEnabled(true);
}                       

我認為,一旦將按鈕設置為禁用,它們將不再可觸摸。 因此,您需要在代碼中修改該點,並使用其他方式來獲取所有視圖。 您可以保留用於禁用按鈕的ArrayList ,然后使用它來重新啟用它們。

編輯:

嘗試這個:

ArrayList<View> touchables //declare globaly

然后

TableLayout tl = (TableLayout)findViewById(R.id.table1); // 
touchables = tl.getTouchables();
for(View touchable : touchables)
{
    if( touchable instanceof Button && touchable != btnNewWord )
      ((Button)touchable).setEnabled(false);
}

現在,

btnNewWord.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

       for(View touchable : touchables)
       {
          if( touchable != btnNewWord )
            ((Button)touchable).setEnabled(true);
       }  
   }
}                     

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM