一、图形界面部分代码:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_writ"
android:singleLine="false"
android:hint="请输入数据"
android:imeOptions="actionDone" />// 监听回车键
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="开始解析数据"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_v"
android:text="显示数据"/>
![](https://images2015.cnblogs.com/blog/1119724/201703/1119724-20170327231452467-1249012973.png)
二、核心功能代码:
private void Start() {
int wd = 0;//单词数
int by = 0;//字符数
int sentence = 0;//句子数
int lines = 0;//行数
char num = 10; //Ascii换行
char nums = 13; //Ascii回车
String s = et_writ.getText().toString().trim();//获取数据
for (int i = 0; i < s.length(); i++) { //得到所输入的数据的长度,并遍历
char c = s.charAt(i);
if (c == ' ') wd++; //单词
if (c==num ||c==nums) ++lines;
if (c == '.' || c == '!' || c == '?')
if (i > 0 && s.charAt(i - 1) != '.' && s.charAt(i - 1) != '?' && s.charAt(i - 1) != '!') {
sentence++;
wd++;
}
by++;
}
++lines;
tv_v.setText("字符:"+by+"\n单词:"+wd+"\n句子:"+sentence+"\n行数:"+lines);
}
}