我想通过Speech to text(STT)在Listview中搜索。 我有一个城市列表,现在是STT的用户搜索。 此搜索仅适用于我的列表视图(如STT联系人搜索),
答案 0 :(得分:1)
Speech to Text内置于Android 1.6+中。这是一个如何做的简单示例。
/**
* Showing google speech input dialog
* */
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
更多信息:http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/
<强>更新强>
请尝试使用此代码进行过滤视图。
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
答案 1 :(得分:0)
请参阅此代码Google Voice in our Application
/* Google Voice open while calling Activity */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/* Call Activity to Open Google Voice */
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, 1);
} catch (ActivityNotFoundException a) {
Log.d("LOG",a.getMessage());
}
}
/* This will be called after you speak */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1: {
if (resultCode == Activity.RESULT_OK && null != data) {
ArrayList text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Log.d("LOG","You have speak : "+text.get(0));
}
break;
}
}
}
您将获得text.get(0)
,因为您可以在过滤ListView
时使用拼写字符串。
notifyDataSetChanged()
对于使用新数据更改ListView
非常有用。