Android development: Count EditText Lines on textChanged?

How to count the number of lines in an EditText? Mostly in my application, I have line numbers, and I wanted to make them update to textchange (I already have setupchangelistener). Is this possible ?: (

Thanks, Alex!

+3
source share
2 answers

The lines may be different:

  • Visible lines: wrapped text as a new line ...
  • List item: only lines with \ r, \ n, \ r \ n

The first case (the simplest):

int nbLines = editText.getLineCount();

Second case:

        int nbLines = 0;
        StringReader     sr = new StringReader(editText.getText().toString());
        LineNumberReader lnr = new LineNumberReader(sr);
        try { 
            while (lnr.readLine() != null){}
            nbLines = lnr.getLineNumber();
            lnr.close();
        } catch (IOException e) {
            nbLines = editText.getLineCount();
        } finally {
            sr.close();
        }
+10
source

, " ". edittext "GUI way", , ? " " (\n )? , . : \n , , - \n.

0

Source: https://habr.com/ru/post/1787573/


All Articles