Saturday 15 August 2015

android - I want to create two button clickers that add up different numbers -



android - I want to create two button clickers that add up different numbers -

if press 1 button , press other, amount adds on both textviews. want them separate numbers. tried google find solution couldn't find anything. please help?

public class mainactivity extends activity { int clicked = 0; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.layout_main); { final textview text = (textview) findviewbyid(r.id.textview2); final imagebutton button = (imagebutton)findviewbyid(r.id.imagebutton2); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub clicked++; text.settext(" " + clicked + " "); } }); } final textview text = (textview) findviewbyid(r.id.textview1); text.settext(""); final imagebutton button = (imagebutton)findviewbyid(r.id.imagebutton1); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub clicked++; text.settext(" " + clicked + " "); } }); } }

use 2 different variables. clicked beingness added when press either button. take different names buttons, don't name them both button.

ex:

public class mainactivity extends activity { int clicked1 = 0; int clicked2 = 0; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.layout_main); { final textview text = (textview) findviewbyid(r.id.textview2); final imagebutton button2 = (imagebutton)findviewbyid(r.id.imagebutton2); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub clicked2++; text.settext(" " + clicked2 + " "); } }); } final textview text = (textview) findviewbyid(r.id.textview1); text.settext(""); final imagebutton button1 = (imagebutton)findviewbyid(r.id.imagebutton1); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub clicked1++; text.settext(" " + clicked1 + " "); } }); } }

android eclipse button count click

No comments:

Post a Comment