java - 如何在一项活动中使用语音向具有多个编辑文本的 Google API 发送文本?
问题描述
我正在使用这段代码向谷歌语音框发送意图
imageViewforvoicetotextlocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Something");
try {
startActivityForResult(i, 2);
} catch (ActivityNotFoundException e) {
Toast.makeText(try_menu_addcustomers.this, "Google voice Search Not Found", Toast.LENGTH_SHORT).show();
}
}
});
使用它为另一个edittext发送另一个意图
imageViewforvoicetotextname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "hi-IN");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Something");
try {
startActivityForResult(intent, 1);
} catch (ActivityNotFoundException e) {
Toast.makeText(try_menu_addcustomers.this, "Google voice Search Not Found", Toast.LENGTH_SHORT).show();
}
}
});
使用此代码块获取和设置数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
switch (requestCode) {
case 1:
if (requestCode == 1 && null != data) {
editText.setText(result.get(0));
}
if (requestCode == 2 && null != data) {
editText3.setText(result.get(1));
}
break;
}
第二个 if 语句从未被触发,我不知道为什么。我做错了什么还是有其他方法可以做这些事情?
解决方案
推荐阅读
- python-3.x - 在 Pycharm 中设置现有的 Python3.6 环境/包
- php - update_batch 中的多个 where / 键
- python - 尝试在opencv上裁剪图像时出现TypeError
- google-apps-script - 如何在谷歌电子表格中跨工作表(标签)复制带有过滤器中值的单元格格式?
- oracle-apex - 100 页应用程序中两个页面的公共区域
- mongodb - mongodb查找层次结构是否存在
- azure-devops - 如何为 Azure 管道创建仅推送触发器
- php - 在 PhpStorm 版本 2019.2.4 中,类名出现错误
- html - Bootstrap,如何使容器流体的高度为 100%
- ios - 如何在 Xcode 中创建动态包标识符