java - 使用语音识别从 TextView 中删除最后一个词

标签 java android textview

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

相关文章:

java - android.view.InflateException : Binary XML file line #0: Error inflating class null

Android setError ("error") 在 Textview 中不起作用

Java JSlider精度问题

java - 我的某些 jsp 页面在 Firefox 中无法运行,但这些页面在 IE 中运行良好?

java - androidfragment - 如何膨胀布局然后制作UI功能

java - 警报对话框内的 TextView 不会向下滚动

android - 带有包含网站链接的文本的自动垂直滚动 TextView

Java大文件保存在数据库中-对象设计

java - 使用 java.nio.file 功能创建文件

android - 在android中填充编辑文本的语音输入?