繁体   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