Button Text and Booleans (Simple Android App) -
i'm build fun facts app on android development track. decided take exploratory detour , try create basic introductory message user. changed facttextview text "you can click button below see new fact!" , changed showfactbutton text "try out!"
from there, changed final line onclick object (is object?) following:
public void onclick(view view) { string fact = mfactbook.getfact(); // update label our dynamic fact factlabel.settext(fact); // set button text new fact prompt showfactbutton.settext("show fun fact.");
this seems work fine. however, feel "updating" button text same new string on every press isn't best practice, if easy , readable. tried add boolean check text of button, , update if has not been updated. i've come far:
view.onclicklistener listener = new view.onclicklistener() { @override public string launchtext = getresources().getstring(r.string.start_text); public string nexttext = getresources().getstring(r.string.next_text); public string buttontext = (string) showfactbutton.gettext(); public boolean updatelaunchtext() { if (buttontext.equals(launchtext)) { buttontext.replaceall(launchtext, nexttext); return true; } else { return true; } } public void onclick(view view) { string fact = mfactbook.getfact(); // update label our dynamic fact factlabel.settext(fact); } };
with following added strings.xml:
<string name="start_text">try out!</string> <string name="next_text">show fun fact!</string>
no errors, button text stays on "try out!" i'm sure objects totally unnecessary compared first, working method scope of app, i'd still figure out since don't have idea i'm doing boolean.
questions: 1) missing in longer boolean approach? 2) what's actual efficient approach accomplish task?
did connect listener button object?without connection no logic applied button click.it goes this:
buttonname.setonclicklistener(...)
you'd have initialize button object first though :)
Comments
Post a Comment