android将语音设置为两个编辑文本的文本

时间:2017-09-12 15:03:34

标签: java android

我创建了两个编辑文本。我创建了两个按钮。通过单击按钮,它将要求用户说话,它会将语音转换为文本,它会  显示在编辑文本中。

我已经将语音调用了两次文本功能。一个用于第一个编辑文本,另一个用于第二个编辑文本但它显示错误。 请帮我解决这个问题。

这是我的代码:

private void promptSpeechInput1() {
    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();
    }
}

/**
 * Receiving speech input
 * */
@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);
                txtSpeechInput1.setText(result.get(0));

            }
            break;
        }

    }
}



//////////////


private void promptSpeechInput2() {
    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();
    }
}

/**
 * Receiving speech input
 * */
@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);
                txtSpeechInput2.setText(result.get(0));
            }
            break;
        }

    }
}

错误显示在该行:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

1 个答案:

答案 0 :(得分:0)

您正在重新定义onActivityResult方法。仅为您的目的使用一个实例

/**
 * Receiving speech input and output
 * */
@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);
            txtSpeechInput1.setText(result.get(0));

        }
        break;
     }
     case REQ_CODE_SPEECH_OUTPUT: {
        if (resultCode == RESULT_OK && null != data) {

            ArrayList<String> result = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            txtSpeechInput2.setText(result.get(0));
        }
        break;

    }



   }
}

并在promptSpeechInput2方法中修改它。使用不同的请求代码,即不是REQ_CODE_SPEECH_INPUT,否则使用具有不同值的REQ_CODE_SPEECH_OUTPUT

try {
    startActivityForResult(intent, REQ_CODE_SPEECH_OUTPUT);
} catch (ActivityNotFoundException a) {
    Toast.makeText(getApplicationContext(),
            getString(R.string.speech_not_supported),
            Toast.LENGTH_SHORT).show();
}