android- react when 2 button clicked -


i want build simple memory game. used 16 button. know how react when specific button cliked, how can react each button click , check if match button was selected (not neccesrly now)?

in xml, use same name onclick each button

<button    android:id="@+id/btn1"    ...    android:onclick="btnclick"/> <button    android:id="@+id/btn2"    ...    android:onclick="btnclick"/> 

and in java code make sure function public, has same name defined in xml's onclick property above, , takes view parameter. view button clicked can id , switch on that, use if/else or want handle that

public void btnclick(view v) {     switch(v.getid())    // v btn clicked give id     {         case (r.id.btn1):   btn1 clicked          ... stuff 

to answer first question, can use flag suggested earlier or counter , if counter == 2 click whatever need. if doesn't store value of button represents compare in second click

the other way set them in for loop, assume know how set loop keep short

for (int i=0; i<buttons.size(); i++) {      ...      button[i].setonclicklistener(activityname.this); }  @override public void onclick(view v) {     int id = v.getid(); } 

make sure implement onclicklistener()


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -