有没有办法以编程方式在软键盘上启动语音输入? [英] Is there a way to launch voice input on the softkeyboard programatically?
问题描述
这个想法是通过按下按钮来获取用户语音输入,并将其传递到应用程序内的搜索中.由于设备限制,我们使用的是 SearchViewCompat 有用的SearchView.我已经能够使用此链接来获取语音输出,但是没有一种简单的方法可以将文本通过这种方式传递到搜索栏.
搜索视图本身显示为一个视图(因此没有setText()),但是如果我只能说在键盘上使用mic键"或在搜索栏出现时立即说些什么,我认为它可能会起作用.任何帮助将不胜感激.
谢谢,
SGB.
The idea is to take a user voice input on the press of a button and pass it to the search inside the app. Because of device limitations we are using the SearchViewCompat instead of the useful SearchView. I have been able to grab the voice output using this link, but there is not a simple way to pass the text to the search bar this way.
The search view itself shows as a view (so no setText()), but if I can just say "use mic key on keyboard" or something as soon as the searchbar comes up, I think it might work. Any help would be greatly appreciated.
Thanks,
SGB.
推荐答案
很抱歉回答了我自己的问题,但在这里是:
我从问题的链接中删除了整个从语音输入中获取一个字符串数组",然后,我这样做不是只用onSearchRequested()
开始搜索,而是这样做了:
Sorry to have answered my own question but here it was:
I took the whole "grab a string array from voice input" off of the link in the question, then, instead of just starting a search with onSearchRequested()
, I did this:
startSearch(grabString, false, null, false);
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(aViaBuildConfig.MIC_KEY) {
DebugLog.e(TAG , "onDown event : " + event);
DebugLog.e(TAG , "onDown keyCode: " + keyCode);
if(keyCode == Constants.MIC_KEY) {
onSearchRequested();
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
try {
startActivityForResult(voiceIntent, Constants.RESULT_SPEECH);
} catch (ActivityNotFoundException ex) {
DebugLog.e(TAG, "Not found excpetion onKeyDown: " + ex);
}
}
return super.onKeyDown(keyCode, event);
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case Constants.RESULT_SPEECH:
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> spokenSearch = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
DebugLog.e("Glenn: " , "Speech = " + spokenSearch);
String grabString = spokenSearch.get(0);
startSearch(grabString, false, null, false);
}
break;
}
}
这篇关于有没有办法以编程方式在软键盘上启动语音输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!