Unable to implement textwatcher correctly in android -
hey guys looking way find out user typing in edittext
, stumbled upon textwatcher
. problem don't understand parameters refer in function. want keep track of text being entered user letter letter. can't figure out argument correct subscript no. have tried different variations haven't had luck those. essentially want save text entered in array until user presses ','button or exceeds limit of 24 characters. please advise.
public class main extends activity implements onclicklistener { // inputans.setonkeylistener(this); button go; textview answer; textview check; edittext inputans, inputq; boolean done1 = false; char[] ans = new char[30]; string predef = "please answer question"; char[] predefc = predef.tochararray(); int len = predefc.length; int = 0; char[] substring = new char[30]; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initialisevars(); go.setenabled(false); go.setonclicklistener(this); inputans.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { if ((count - before) > 0) { char current = s.charat(before); if (current == '.' || >= 24) { // 24 check // ans[i]='\0'; // inputans.settext(predef, // textview.buffertype.editable); check.settext(predef); go.setenabled(true); } else { ans[i] = current; i++; // char[] substring = char[30]; button // problem,check if prev length - current length // >0 int m; (m = 0; m <= i; m++) { substring[m] = predefc[m]; } check.settext(substring.tostring()); // substring[m]='\0'; /* * inputans.settext(substring.tostring(), * textview.buffertype.editable); */ } } } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } }); } private void initialisevars() { // todo auto-generated method stub go = (button) findviewbyid(r.id.bgo); answer = (textview) findviewbyid(r.id.tvans); inputans = (edittext) findviewbyid(r.id.ettricky); inputq = (edittext) findviewbyid(r.id.etques); check = (textview) findviewbyid(r.id.textview1); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { case r.id.bgo: { answer.settext(ans.tostring()); } break; } } }
in android documentation, can read that:
public abstract void ontextchanged (charsequence s, int start, int before, int count) added in api level 1
this method called notify that, within
s
,count
characters beginning @start
have replaced old text had lengthbefore
. error attempt make changes s callback.
it means, that:
s
modified text (you can it's length,s.length()
)count
number of characters have been addedstart
position in new characters have been addedbefore
length of text before changes
basically achieve want, i'd rather go approach instead of arrays:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initialisevars(); go.setenabled(false); go.setonclicklistener(this); inputans.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { if ((s.length() - before) > 0) { // check if text longer before changing char current = s.charat(before); if (current == '.' || s.length() >= 24) { check.settext(predef); go.setenabled(true); } else { check.settext(s); } } } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } }); }
simple that.
Comments
Post a Comment