我有一个EditText
,最长可达140个字符。现在我有一个按钮,当我点击按钮时,编辑文本的内容应该打印在log cat上。到目前为止,我已经实施了。
但是我的问题是,我应该验证单词,这意味着没有意义的连续文本(例如“ajldjlkdjkjfdjdjjd”)不应该被打印而应该检查单词长度为10的约。用于语法。怎么实现这个?任何想法对我都有帮助。提前谢谢。
注意: - 我可以输入单词和数字,如果我输入的单词没有意义,它应该提醒消息。
答案 0 :(得分:4)
您可以使用下面的代码段。在长度合适时添加代码。
//Prepare object of Edit Text
EditText editText = (EditText) findViewById(R.id.editText1);
//Get String entered by user
String enteredText = editText.getText().toString();
//split string to get every word using _ (space) and add all word to an array
String[] words = enteredText.split(" ");
for (int i = 0; i < words.length; i++) {
if (words[i].length() > 10) {
//handle if any word is > 10 ch.
break;
}else{
//handle if word is meaning full or not
}
}
答案 1 :(得分:0)
没有简单的方法来实现这一目标。(坦率地说几乎不可能。)
一种解决方案可以是 - 使用单词列表保留本地数据库,并根据单词长度进行排序。
现在,每次从编辑文本中获取一些文本时,请检查字符串的大小。如果它等于10,则首先比较DB中长度= 10的所有单词。 您无法比较输入的文本的子集。
示例知识可以被视为知道,现在,壁架,边缘,领导,猫头鹰等。
您必须为字子集查找实现自己的(复杂)逻辑。