我正在使用这段代码向谷歌语音框发送意图
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 语句从未被触发,我不知道为什么。我做错了什么还是有其他方法可以做这些事情?