我在这里有活动。当用户触摸活动的布局时,我希望应用程序要求用户提供五条信息。为了实现这一点,出现了一系列五个语音输入提示。下面,我有这个代码:
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;
}
}
我面临的问题是,当我触摸布局时,只会发生一个语音提示,提示音频输入。如何才能调出每个提示?
编辑:我现在让OnActivityResult中的每个函数互相调用。但是,我仍然会无限期地保持语音输入。
答案 0 :(得分:1)
您需要调用 promptSpeechInput_win()
在promptSpeechInput_town的onActivityResult
等等。理想情况下,只能从用户获取一个语音输入。因此,您应该在之前的语音请求的onActivityResult上启动下一个。
此外,您需要打破开关盒,以避免每次都执行所有情况。