简体   繁体   中英

How to inactivate buttons that have not been pressed

I'm a very unexperienced programmer, so please bear with me for my lack of knowledge.

I have a program with 168 different buttons, which each count how many times they have been each pressed. After having either pressed or not pressed all of the buttons i need to inactivate and grey-out those which have not been pressed. So far I've used an 3D array to store how many times each button has been pressed, and have made a simple code:

if(a[0][0][0]<1)
{
    ImageButton button_a1=(ImageButton) findViewById(R.id.button1a);
    button_ca1.setEnabled(false);
    button_ca1.setAlpha(6);
}

The only problem is that since each buttonID is different i have to do this 168 separated times. Is there any way to make this into a simple loop that doesn't take up over 1000 lines of code?

The program is written using Eclipse and is used for an Android app.

If all the buttons are in same layout, you can use getChildCount() :

LinearLayout layout = setupLayout();
int count = layout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
    v = layout.getChildAt(i);
    if (v instanceof ImageButton) {
        v.setEnabled(false);
        v.setAlpha(6); 
    }
}

If not, simply create a Set , List or Array with all buttons and iterate over it:

List<ImageButton> buttons = new ArrayList<ImageButton>();

// be sure buttons is visible in the method
// when creating the buttons put them inside the list

Then in your method:

if(a[0][0][0]<1)
{
    // disable all buttons
    for(ImageButton button : buttons) {
        button.setEnabled(false);
        button.setAlpha(6);
    }
}

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