2012-03-05 37 views
3

我正在构建一个使用语音识别的应用程序..我将最后一个句子存储在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

+0

如何进行代码的行为,现在,有没有异常?或者它没有找到任何匹配?请让我们知道发生了什么 – Jimmy 2012-03-05 20:19:16

+0

先生我想删除最后一个字,当用户说“你好”,像一个退格.. – Shah 2012-03-05 20:42:59

回答

2

使用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); 
} 
+0

谢谢先生..我的问题几乎解决..但你能帮助我休息的代码..我仍然有这个问题... – Shah 2012-03-05 20:41:15

+0

更新了答案。 – 2012-03-05 21:58:08

+0

谢谢大家..我的问题现在解决.. :) – Shah 2012-03-06 07:48:28