2

是否可以以编程方式打开“立即发言”对话框?

目前,如果用户点击我的“搜索”按钮,则会打开一个对话框,并且我会自动打开软键盘,因此用户无需点击 textedit 字段。

我想提供一个替代的“语音搜索”,它会打开对话框并自动打开“现在说话”窗口。因此用户不必在键盘上找到并点击“麦克风”按钮。

有任何想法吗?

4

1 回答 1

4

对的,这是可能的。查看ApiDemosAndroid SDK 中的示例。有一个名为 的活动VoiceRecognition,它利用RecognizerIntent.

基本上,你需要做的就是用一些额外的东西来创造一个适当的意图,然后阅读结果。

private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

private void startVoiceRecognitionActivity() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    // identifying your application to the Google service
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
    // hint in the dialog
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
    // hint to the recognizer about what the user is going to say
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    // number of results
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
    // recognition language
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"en-US");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
        // do whatever you want with the results
    }
    super.onActivityResult(requestCode, resultCode, data);
}
于 2012-10-14T22:24:32.770 回答