英文:
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
<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"/>
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 < 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()); // 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(""\n"", """)`。
最终代码:
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 < noteLineNumber; i++) {
endIndex = layout.getLineVisibleEnd(i);
String[] temp = noteText.subSequence(startIndex, endIndex).toString().trim().replaceAll(""\n"", """").split("" +"");
wordNumberPerLine = temp.length;
if (temp.length != 0)
if (temp[0].length() == 0)
wordNumberPerLine--;
text.append(wordNumberPerLine).append(""\n"");
startIndex = endIndex;
}
textView.setText(text.toString());// I show the result in a textView
}
另外,请记住 `"".split("" "")` 返回1,所以如果需要的话,您可能需要检查空字符串。
英文:
You are on the right track. You can remove all linefeeds \n
from the resulting string.
Just add .replace("\n", "")
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 < noteLineNumber; i++) {
endIndex = layout.getLineVisibleEnd(i);
String[] temp = noteText.subSequence(startIndex, endIndex).toString().trim().replaceAll("\n", "").split(" +");
wordNumberPerLine = temp.length;
if (temp.length != 0)
if (temp[0].length() == 0)
wordNumberPerLine--;
text.append(wordNumberPerLine).append("\n");
startIndex = endIndex;
}
textView.setText(text.toString());// I show the result in a textView
}
Also, remember that "".split(" ")
returns 1, so maybe you need to check for an empty string if necessary
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论