1

我尝试使用下面链接中提到的代码来“批量禁用”按钮,它工作得很好。但是,相同的代码不适用于批量启用。

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);
}                       
4

1 回答 1

3

一旦你将按钮设置为禁用,我想,它们将不再是可触摸的。因此,您需要在代码中修改该点并使用其他内容来获取所有视图。您可以保留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);
       }  
   }
}                     
于 2011-10-18T06:11:35.157 回答