简体   繁体   English

Android:批量启用按钮已被禁用

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

I have tried using the code mentioned in the link below to "mass disable" buttons and it works perfectly fine. 我已经尝试使用下面链接中提到的代码“质量禁用”按钮,它工作得很好。 However the same code does not work for mass enable. 但是,相同的代码不适用于批量启用。

Android: mass enable/disable buttons Android:批量启用/禁用按钮

Code for Disable (Working) 禁用代码(工作)

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

CODE for Enable (Not Working) 启用代码(不工作)

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

Once you set the buttons disabled,i think,they will no longer be touchable. 我认为,一旦将按钮设置为禁用,它们将不再可触摸。 So you need to modify that point in your code and use something else to get all views. 因此,您需要在代码中修改该点,并使用其他方式来获取所有视图。 You can preserve your ArrayList which you used to disable buttons and then use the same to re enable them. 您可以保留用于禁用按钮的ArrayList ,然后使用它来重新启用它们。

EDIT : 编辑:

Try this: 尝试这个:

ArrayList<View> touchables //declare globaly

then 然后

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

and now, 现在,

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