Android/Java EditableTextView Line Count -
i trying teach myself android java programming , have started attempting create simple text editor.
i wanted have line count down left hand side standard ides, , couldn't find anywhere on stackexchange or internet on definitive "best practice" way this.
so created own logic based on read, wanted check best , efficient way -- , if happens out looking same thing.
// start oncreate // @medittext = main autocompletetextview // @mlinecount = line count textview medittext.addtextchangedlistener(new textwatcher() { // set current line variable private int currentline; // text watcher public void beforetextchanged(charsequence s, int start, int count, int after) { // before new text inserted, current line count currentline = medittext.getlinecount(); } public void ontextchanged(charsequence s, int start, int before, int count) { // nothing } @override public void aftertextchanged(editable e) { // update line count // @medittext: autocompletetextview input // @mlinecount: textview output // @currentline: integer updatelinecount(medittext, mlinecount, currentline); } }); // end oncreate public void updatelinecount(autocompletetextview edittext, textview linetext, int currentline){ // updated line count int linecount = edittext.getlinecount(); // if line count exists , not "before" line count (to stop repeating) if(linecount > 0 && linecount != currentline){ // if "before" line count smaller, push line count if(currentline < linecount){ linetext.append(integer.tostring(linecount) + "\n"); } // else if "before" line count greater (ie. have deleted line), push line count down else { // text of current linetext textview, replace substring of // current linetext textview - length of deleted line // (ie. line 9 = 1 character + 1 line break; line 10 = 2 characters + 1 etc) linetext.settext(linetext.gettext().tostring().substring(0, linetext.gettext().tostring().length() - (integer.tostring(linecount+1).length() + 1))); } } return; }
so yeah, working fine -- not sure last line -- seems abit ... resource wasteful .. replacing entire linetext textview content each time line deleted.
is there anti-append might work better in situation?
thanks, jamie
if(linecount > 0 && linecount != currentline){
what happend if select text , delete all.
not meet condition.
so, mabe text line still display "5 lines" rather "0 line"
Comments
Post a Comment