我正在构建一个使用语音识别的应用程序。我将最后一句话存储在 textView 的字符串中,并尝试将它与说出的新词进行比较,例如,当用户说“删除”应删除 TextView 中的字符串..
我不知道这段代码有什么问题..
if(requestCode == request_code && resultCode == RESULT_OK)
{
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(matches != null && matches.size() > 0)
{
text = matches.get(0);
if(text == "hello")
{
text = (String) et.getText();
rep = text.replaceAll("\\d+\\Z", "");
Log.d(tag, "THis is not working");
et.setText(rep);
rep = null;
}
else
{
option = matches.get(0);
et.setText(option);
}
}
提前致谢:D
最佳答案
使用 if(text.equals("hello"))
而不是 if(text == "hello")
或者甚至更好:if(text.equalsIgnoreCase("hello"))
从文本区域中删除最后一个词:
String text = textArea.getText();
// capture everything except the last word into group 1
Pattern regex = Pattern.compile("(.*?)\\w+\\s*\\Z", Pattern.DOTALL);
Matcher matcher = regex.matcher(text);
if (matcher.find()) {
text = matcher.group(1);
textArea.setText(text);
}
关于java - 使用语音识别从 TextView 中删除最后一个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9573250/