I'm making an app in Android studio and I have buttons for categories from 1 to count
. This is how I created them:
category1 = (Button) findViewById(R.id.cat1Btn);
category1.setOnClickListener(this);
category2 = (Button) findViewById(R.id.cat2Btn);
category2.setOnClickListener(this);
...
What I want to do now is loop through each button and leave only the first button enabled. So I want to disable them from i
to count
like so:
for (int i=userProgress+2; i<=count; i++){
String categoryName = "category" + i;
categoryName.setEnabled(false);
}
This obviously doesn't work because it doesn't refer to a specific button, it's just a String. But I hope this shows you what I am trying to achieve. Basically I just need to figure out a way how to use an index to reference a button. How could I go about doing this?
Any help is appreciated. Thank you!
I recommend you keep all these buttons in a data structure such as an array. Then, iterate over each one of them in a loop, and as you write earlier: button[i].setEnabled(false);
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.