android - Detect what button was pressed? -
i have 26 different buttons containing alphabets. in want if button contains d, i, l should show image button if other button clicked else happens. below have tried doesn't seem working. aware of ontouchkeylistener() might huge of coding.
imgd.setvisibility(view.invisible); imgi.setvisibility(view.invisible); imgl.setvisibility(view.invisible); if(d.ispressed()) { imgd.setvisibility(view.visible); }
you can set tag each button , use key map string image button. way can use single click listener (or onclick method) , appropriate image. in code, use settag(object) method. in xml, can use android:tag="value" attribute.
map<object, imagebutton> buttonmap = new hashmap<object, imagebutton>(); // initialize map // later: public void onclick(view v) { imagebutton btn = buttonmap.get(v.gettag()); if (btn != null) { btn.setvisibility(view.visible); } } instead of map, set button tags , image button tags , use findviewwithtag() find image button corresponding clicked button.
public void onclick(view v) { view btn = findviewwithtag("img" + v.gettag()); if (btn != null) { btn.setvisibility(view.visible); } }
Comments
Post a Comment