android - Recognizing a button from a dynamic view -


i have written code dynamic layout using loop generate pair of buttons (this part of code generate them)

  for(int = 1; <= 2 ; i++) {         button button1 = new button(this);         button1.settag("age");         button1.setid(i);         layout.addview(button1);          button button2 = new button(this);         button2.setid(i);         button2.settag("country");         button2.setenabled(false);         layout.addview(button2);          button1.setonclicklistener(this);         button2.setonclicklistener(this);        } 

what wish if button1 clicked, button2 should enabled (initially disabled).

this easy task if buttons created in xml have separate r.id.xxxxx names each, here unable understand how detect other button in onclick(view v) method can change if enabled or not, have tried add tag each button have parameter recognize buttons have no idea how recognize other button view information of clicked button1.

i assume using button tags in click processing. keep tag data , add needed wiring between buttons, can create data structure serve tag:

static class buttontag {     string buttontype;     button partner;     buttontag(string type, button button) {         buttontype = type;         partner = button;     } } 

then reorganize setup code:

for(int = 1; <= 2 ; i++) {     button button1 = new button(this);     button1.setid(i);     layout.addview(button1);      button button2 = new button(this);     button2.setid(i);     button2.setenabled(false);     button1.settag(new buttontag("age", button2));     button2.settag(new buttontag("country", button1));     layout.addview(button2); } 

the click processing need changed cast gettag() buttontag instead of string.

if don't need "age" , "country" information distinguish button types, set each button tag other.

edit:

with latter scheme, here's how use in click listener:

public void onclick(view v) {     object tag = v.gettag();     if (tag instanceof button) {         button btn = (button) tag;         btn.setenabled(true);         v.setenabled(false);     } } 

if needed "age" , "country" part of tag other reasons, code little different:

public void onclick(view v) {     object tag = v.gettag();     if (tag instanceof buttontag) {         buttontag btag = (buttontag) tag;         btag.partner.setenabled(true);         v.setenabled(false);     } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -