android - How to make a button give different results everytime when user tap it? -


i have button , want change text positioned above button random everytime user press it. how can it?

i want this:

"hello"

press

"why did press?"

press

"don't again, or..."

press

"you did!"

.

.

.

here code of button , text.

dontpressbutton.setonclicklistener(             //sets button wait press             new button.onclicklistener(){                 public void onclick(view v) {                     //selects text field changed                     textview textchange = (textview) findviewbyid(r.id.textchange);                     //changes text                     textchange.settext(string.textchange2);                 }             }     ); 

there no magical way have randomly change text, have program that. going have create array of responses , in button's code, tell cycle through responses. example:

private string[] responsearray = {"hello", "why did press?", "don't again", "you did!" } private int numtimespressed = 0   dontpressbutton.setonclicklistener(             //sets button wait press             new button.onclicklistener(){                 public void onclick(view v) {                     //selects text field changed                     textview textchange = (textview) findviewbyid(r.id.textchange);                     //changes text                     //note line cause error if there not enough values in array. have write catch                     textchange.settext(responsearray[numtimespressed++]);                     //if want random, you'll have change array index accessing random value between array's bounds                 }             } ); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -