Answer the question
In order to leave comments, you need to log in
How to make one variable for several buttons?
Good day!
We have 22 buttons in 1 layout1 . When you press any of these buttons, the same Activity2 and the layout2 attached to it will be activated . But depending on the pressed button in layout1 , the content of the text in layout2 will change . And now, dear experts, a question. How not to prescribe the action of each button, but simply make the OnClickListener listen for any click and and perform a specific action? It's just that if everything is templated, I doubt that the developer will code each button, which will only differ in id and text that will be displayed in layout2 .
Thank you!
Answer the question
In order to leave comments, you need to log in
for(int i=0; i < layout.getChildCount(); i++){
View v = layout.getChildAt(i);
if (v instanceof Button) {
Button button = (Button) v;
button.setOnClickListener(this);
}
}
I doubt that the developer will code each button, which will only differ in id and text
OnClickListener
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question