屏幕上的多个语音输入

Multiple Voice Inputs on a screen

我这里有一个activity。当用户触摸 activity 的布局时,我希望应用程序要求用户提供五条信息。为了实现这一点,出现了一系列五个语音输入提示。下面,我有这个代码:

    package com.example.shivamgandhi.gyrosafe;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.Locale;

public class Memory_Test1_Activity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {

    EditText ed23, ed24, ed25, ed26, ed27;
    private final int REQ_CODE_SPEECH_INPUT_TOWN = 100;
    private final int REQ_CODE_SPEECH_INPUT_WIN = 101;
    private final int REQ_CODE_SPEECH_INPUT_MONTH = 102;
    private final int REQ_CODE_SPEECH_INPUT_DAY = 103;
    private final int REQ_CODE_SPEECH_INPUT_TEAM = 104;
    int n = 1;
    Button btnarray[] = new Button[n];
    public static final String MyPREFERENCES = "MyPrefs";
    SharedPreferences sharedpreferences;
    RelativeLayout RelativeLayout;

    int count = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memory_test1);

        Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        btnarray[0] = (Button)findViewById(R.id.button8);

        sharedpreferences = this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        ed23 = (EditText)findViewById(R.id.editText23);
        ed24 = (EditText)findViewById(R.id.editText24);
        ed25 = (EditText)findViewById(R.id.editText25);
        ed26 = (EditText)findViewById(R.id.editText26);
        ed27 = (EditText)findViewById(R.id.editText27);

        RelativeLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);

        for(int i = 0; i <n; i++){
            btnarray[i].setOnClickListener(this);
        }

        RelativeLayout.setOnTouchListener(this);
    }

    private void promptSpeechInput_town() {
        Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speech_prompt));
        try {
            startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TOWN);
        }
        catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void promptSpeechInput_win() {
        Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speech_prompt));
        try {
            startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_WIN);
        }
        catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void promptSpeechInput_month() {
        Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speech_prompt));
        try {
            startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_MONTH);
        }
        catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void promptSpeechInput_day() {
        Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speech_prompt));
        try {
            startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_DAY);
        }
        catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void promptSpeechInput_team() {
        Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speech_prompt));
        try {
            startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TEAM);
        }
        catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }

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

        switch (requestCode) {
            case REQ_CODE_SPEECH_INPUT_TOWN: {
                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result_twn = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed23.setText(result_twn.get(0));
                    promptSpeechInput_win();
                }
            }

            case REQ_CODE_SPEECH_INPUT_DAY:{
                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result_day = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed26.setText(result_day.get(0));
                    promptSpeechInput_team();
                }
            }

            case REQ_CODE_SPEECH_INPUT_WIN:{
                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result_win = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed24.setText(result_win.get(0));
                    promptSpeechInput_month();
                }
            }

            case REQ_CODE_SPEECH_INPUT_MONTH:{
                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result_month = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed25.setText(result_month.get(0));
                    promptSpeechInput_day();
                }
            }

            case REQ_CODE_SPEECH_INPUT_TEAM:{
                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result_team = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed27.setText(result_team.get(0));
                }
            }
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event){
        if(v == RelativeLayout && count == 0 ){
            promptSpeechInput_town();
            count = 1;
            return true;
        }
        else{
            return false;
        }
    }

我面临的问题是,当我触摸布局时,只有一种语音提示出现,promptSpeechInput_team。我怎样才能调用每个提示?

编辑:我现在让 OnActivityResult 中的每个函数互相调用。但是,我仍然有语音输入无限期地继续。

你需要调用promptSpeechInput_win()

在 promptSpeechInput_town

的 onActivityResult 中

等等..理想情况下只能从用户那里获取一个语音输入。因此,您应该启动上一个语音请求的下一个 onActivityResult。

此外,您还需要打破 switch case 以避免每次都执行所有 case。