2016-03-07 58 views
3

我在這裏有一個活動。當用戶將佈局觸及到活動時,我希望應用程序要求用戶提供五條信息。爲了做到這一點,一系列的五個語音輸入提示出現。下面,我對這個代碼:屏幕上的多個語音輸入

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中的每個函數調用彼此。不過,我仍然有語音輸入無限期地繼續下去。

+1

您需要在'promptSpeechInput_town'的onActivityResult中調用'promptSpeechInput_win()'等。 – Dhina

+0

我使用了您的建議,現在我有多個語音輸入發生。但是,語音輸入將輸入的單詞發送到所有的editTexts,而不是與其配對的單詞。此外,現在有無數的聲音輸入。 – sjgandhi2312

+1

你忘了打開開關盒。 – Dhina

回答

1

你需要調用promptSpeechInput_win()

在onActivityResult promptSpeechInput_town

等..理想的情況下只有一個聲音輸入可以從用戶獲得。因此,您應該啓動下一個onActivityResult先前的語音請求。

此外,您需要打破開關情況,以避免每次都執行所有情況。