在EditText中每行的单词数不正确。

huangapple go评论88阅读模式
英文:

Bad number of word per line in an EditText

问题

以下是翻译的内容:

我尝试在EditText中实现逐行动态单词机制,但当我按键盘上的换行键时,系统只显示我按下键盘时的一个单词。


我的EditText:

<EditText
    android:id="@+id/note_content_edittext"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:focusable="true"
    android:gravity="top|start"
    android:textSize="17sp"
    android:isScrollContainer="true"
    android:textAlignment="textStart"
    android:inputType="textMultiLine"/>

事件监听器:

note_content_edittext.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        wordPerLineNumber();
    }

    @Override
    public void afterTextChanged(Editable s) {}
});

以及wordPerLineNumber函数:

private void wordPerLineNumber() {
    final String noteText = note_content_edittext.getText().toString();
    Layout layout = note_content_edittext.getLayout();

    int noteLineNumber = layout.getLineCount();
    int endIndex, wordNumberPerLine;

    StringBuilder text = new StringBuilder();

    for (int i = 0, startIndex = 0; i < noteLineNumber; i++) {
        endIndex = layout.getLineVisibleEnd(i);
        wordNumberPerLine = noteText.subSequence(startIndex, endIndex).toString().split(" ").length;

        text.append(wordNumberPerLine).append("\n");
        startIndex = endIndex;
    }

    line_number_textview.setText(text.toString()); // 我在一个textView中显示结果
}

我认为问题可能是因为在按下键盘上的换行键时添加了一个"\r",但我不知道如何解决这个问题。

英文:

I try to put in place a dynamic word per line mechanism in an EditText but the system shows me 1 word when I press my keyboard for a new line.


My EditText

          &lt;EditText
                android:id=&quot;@+id/note_content_edittext&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:background=&quot;@color/white&quot;
                android:focusable=&quot;true&quot;
                android:gravity=&quot;top|start&quot;
                android:textSize=&quot;17sp&quot;
                android:isScrollContainer=&quot;true&quot;
                android:textAlignment=&quot;textStart&quot;
                android:inputType=&quot;textMultiLine&quot;/&gt;

The event listener:

          note_content_edittext.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    wordPerLineNumber();
                }

                @Override
                public void afterTextChanged(Editable s) {}
            });

and the wordPerLineNumber function:

     private void wordPerLineNumber() {
        final String noteText = note_content_edittext.getText().toString();
        Layout layout = note_content_edittext.getLayout();

        int noteLineNumber = layout.getLineCount();
        int endIndex, wordNumberPerLine;

        StringBuilder text = new StringBuilder();


        for (int i = 0, startIndex = 0; i &lt; noteLineNumber; i++) {
            endIndex = layout.getLineVisibleEnd(i);
            wordNumberPerLine = noteText.subSequence(startIndex, endIndex).toString().split(&quot; &quot;).length;

            text.append(wordNumberPerLine).append(&quot;\n&quot;);
            startIndex = endIndex;
        }

        line_number_textview.setText(text.toString()); // I show the result in a textView
    }

I think that my issue is maybe due to the adding of a "\r" when I press for a new line in my keyboard but I don't know how to fix that.

答案1

得分: 1

以下是您要翻译的代码部分:

你现在是在正确的轨道上您可以从结果字符串中删除所有换行符`\n`。
只需在 `wordPerLineNumber()` 方法中添加 `.replace("&quot;\n&quot;", "&quot;&quot;)`。

最终代码:

private void wordPerLineNumber() {
    final String noteText = editText.getText().toString();
    Layout layout = editText.getLayout();

    int noteLineNumber = layout.getLineCount();
    int endIndex, wordNumberPerLine;

    StringBuilder text = new StringBuilder();

    for (int i = 0, startIndex = 0; i &lt; noteLineNumber; i++) {
        endIndex = layout.getLineVisibleEnd(i);
        String[] temp = noteText.subSequence(startIndex, endIndex).toString().trim().replaceAll("&quot;\n&quot;", "&quot;&quot;").split("&quot; +&quot;");
        wordNumberPerLine = temp.length;
        if (temp.length != 0)
            if (temp[0].length() == 0)
                wordNumberPerLine--;
        text.append(wordNumberPerLine).append("&quot;\n&quot;");
        startIndex = endIndex;
    }

    textView.setText(text.toString());// I show the result in a textView
}

另外,请记住 `&quot;&quot;.split("&quot; &quot;")` 返回1所以如果需要的话您可能需要检查空字符串
英文:

You are on the right track. You can remove all linefeeds \n from the resulting string.
Just add .replace(&quot;\n&quot;, &quot;&quot;) to wordPerLineNumber() method.

Final code:

private void wordPerLineNumber() {
    final String noteText = editText.getText().toString();
    Layout layout = editText.getLayout();

    int noteLineNumber = layout.getLineCount();
    int endIndex, wordNumberPerLine;

    StringBuilder text = new StringBuilder();

    for (int i = 0, startIndex = 0; i &lt; noteLineNumber; i++) {
        endIndex = layout.getLineVisibleEnd(i);
        String[] temp = noteText.subSequence(startIndex, endIndex).toString().trim().replaceAll(&quot;\n&quot;, &quot;&quot;).split(&quot; +&quot;);
        wordNumberPerLine = temp.length;
        if (temp.length != 0)
            if (temp[0].length() == 0)
                wordNumberPerLine--;
        text.append(wordNumberPerLine).append(&quot;\n&quot;);
        startIndex = endIndex;
    }

    textView.setText(text.toString());// I show the result in a textView
}

Also, remember that &quot;&quot;.split(&quot; &quot;) returns 1, so maybe you need to check for an empty string if necessary

huangapple
  • 本文由 发表于 2020年5月3日 19:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61573685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定