java - Find and set onClickListener on Buttons using a for loop [Android] -
i have created 10 buttons in layout file , want "find" them , store them in array. want want put different onclicklistener on each of them every button press else. (in case want them submit value 0-9, first button submits 0, second 1, etc). here code have far:
package de.lucbe.tilt; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; public class mainactivity extends appcompatactivity{ button[] buttons = new button[10]; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); for(int = 0;i < buttons.length; i++ ){ string buttonid = "button" + i; int resid = getresources().getidentifier(buttonid, "id", getpackagename()); buttons[i] = ((button) findviewbyid(resid)); } }
}
thank in advance.
for(int = 0;i<buttons.length;i++){ button = buttons[i]; button.setid(i); button.setonclicklistener(this); }
in on click buttons throw there numbers id :)
Comments
Post a Comment