简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM