语音识别安卓应用 [英] speech recognition android application

查看:26
本文介绍了语音识别安卓应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下说话按钮并在列表视图上打印结果时,我的应用程序会识别语音,当我单击结果时,我希望将其打印在吐司上....

my application recognises speech when I press speak button and print results on listview, and when i click on the the result I want it print it on toast....

问题是当我再次单击该按钮时,它会打印新文本和旧文本,例如在我第一次收到时:(不,现在,知道)我单击否并显示现在",第二次它给出(谷歌,好,谷歌)我按好,然后打印现在"然后好"

the problem is when i click the button again it prints the new text and also the old text, for example in the first time i get : (no ,now, know) I click no and it shows "now", the second time it gives (google, good, Google) I press good and it prints "now" then "good"

我觉得它记得整个listview,你能帮我解决这个问题吗,这是代码:

I think it remembers the whole listview, can you please help me to solve this problem, this is the code:

if (resultCode == Result.Ok)
{
    IList<System.String> matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
    voice_list.Adapter = new ArrayAdapter<System.String>(this, Android.Resource.Layout.SimpleListItem1, matches);
    voice_list.ItemClick += delegate(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
    {
        var t = matches[e.Position];

        string TextToTranslate = t.ToString();

        Android.Widget.Toast.MakeText(this, TextToTranslate , Android.Widget.ToastLength.Long).Show();

    }; 
}

推荐答案

这是因为每次处理结果时都要添加一个事件处理程序.因此,您第二次处理语音结果时会向 ItemClick 事件添加另一个事件处理程序.当您单击一个项目时,这将导致 2 个事件处理程序.尝试移动这部分.但也请记住,如果您将其移至 OnStart,当您从另一个屏幕返回时也会调用它.

It's because you're adding an eventhandler everytime you're handling the result. So the 2nd time you're handling the voice result adds another eventhandler to the ItemClick event. That will result in 2 events handlers when you click an item. Try to move this part. But also keep in mind that if you move it to OnStart this will be called also when you're coming back from another screen.

不太确定为什么再次显示第一个,但也许您还必须在更改后将 NotifyDataSetChanged 触发到适配器.

Not really sure why you get the first one displayed again, but maybe you also have to trigger NotifyDataSetChanged to the Adapter after you've changed it.

我个人更喜欢为适配器创建一个新类,从 BaseAdapter 扩展,您将在其中处理带有项目和项目的列表.(例如:您的 ListView 的自定义 ListAdapter)

I personally prefer to create a new class for the adapter, extended from BaseAdapter, where you will handle the list with items and itemsclicked. (example: Custom ListAdapter for your ListView)

这篇关于语音识别安卓应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆