我创建了一个获取文本并计算其中的单词数的应用。我的应用程序具有用于输入文本的“编辑文本”和用于显示单词数的“文本视图”。我必须对我的应用程序提出问题:1.标点符号(例如“!?。”,其后有空格)被视为不应该包含的单词,并且2.从编辑文本中删除文本后,我的文本视图显示1(即数字)字词,但编辑文本为空。请帮忙。
这是我的代码:
package com.farmani.wordcounter;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
et = findViewById(R.id.et);
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@SuppressLint("SetTextI18n")
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text = et.getText().toString();
text = text.replace("\n" , " ");
String[] textArray = text.split(" +|,|\\.");
tv.setText("Number of Words: " + textArray.length);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
答案 0 :(得分:0)
尝试一下:
final String text = "Hello! This is some text with words.";
final String[] split = text.split("\\W"); // Split by "non-word" characters
int wordCnt = 0;
for (String s : split) {
if (!s.isEmpty()) { // If two adjacent non-word characters are encountered the string in between is empty, so we don't count it.
wordCnt++;
}
}
另请参阅https://developer.android.com/reference/java/util/regex/Pattern