android - changing focus on multiple edittext boxes -
i've written code test if user has changed values in of 6 edittext boxes, code looks bulky , redundant. i'm wondering if there more elegant way this.
the user inputs numbers of 6 edittext boxes, , after each entry, calculation routine checks sure boxes have valid numbers before doing calculation. kistener has fire on lost foucus number in box before calculations done. , outputing result. if user want go , change value in 1 of 6 edittext boxes, calulation routine runs again.been i've set 6 listeners in oncreate section each edittext box. have 6 separate routines test each hasfocus==false.
within oncreate section set listener each of 6 user input edottext boxes:
et1.setonfocuschangelistener(this); . . . . et6.setonfocuschangelistener(this);
then have method check if edittext box has lost focus, suggesting theat user changed data, , therfore re-run calulations.
check each of 6 user input edittext boxes follows:
@override public void onfocuschange(view v, boolean hasfocus) { if ((v == findviewbyid(r.id.et1)) && (hasfocus==false)) {do calculations } . . . . if ((v == findviewbyid(r.id.et6)) && (hasfocus==false)) {do calculations } }
forgive me if i've made typos in code above. runs on platform, learn better way this.
i think checking view has lost focus unnecessary, unless calculations different depending on box has lost focus. if calculations same, try:
public void onfocuschange(view v, boolean hasfocus) { if(!hasfocus) { //do calculations } }
if you're caring 1 of 6 has lost focus, time focus changes, check see if it's not focused, perform calculations.
also, reference, it's more efficient check if(v.getid() == r.id.et1)
rather calling findviewbyid()
unnecessarily.
edit: thinking, haven't tested this, might more effective, cause calculations occur if value changed:
private string last; public void onfocuschange(view v, boolean hasfocus) { if(hasfocus) { last = ((edittext)v).gettext().tostring(); } else { if(!((edittext)v).gettext().tostring().equals(last)) { //do calculations } } }
Comments
Post a Comment