2016-03-07 5 views
3

J'ai une activité ici. Lorsque l'utilisateur touche la mise en page à l'activité, je souhaite que l'application demande à l'utilisateur de donner cinq informations. Pour ce faire, une série de cinq invites d'entrée vocale apparaissent. Ci-dessous, je le code pour cela:Entrées vocales multiples sur un écran

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; 
     } 
    } 

Le problème que je suis face est que lorsque je touche la mise en page, seul un des discours invites se produire, promptSpeechInput_team. Comment puis-je faire en sorte que chacune des invites est appelée? Editer: J'ai maintenant chaque fonction dans OnActivityResult appelant les uns les autres. Cependant, j'ai toujours l'entrée vocale continue indéfiniment.

+1

Vous devez appeler 'promptSpeechInput_win()' dans onActivityResult de 'promptSpeechInput_town' et ainsi de suite .. – Dhina

+0

J'ai utilisé votre suggestion, et maintenant j'ai plusieurs entrées vocales qui se passe. Cependant, les entrées vocales envoient les mots d'entrée à tous les editTexts au lieu de simplement celui avec lequel ils sont associés. En outre, il existe maintenant un nombre illimité d'entrées vocales. – sjgandhi2312

+1

Vous avez oublié de casser les boîtiers de commutateur. – Dhina

Répondre

1

Vous devez appeler promptSpeechInput_win()

dans onActivityResult de promptSpeechInput_town

et ainsi de suite .. Idéalement une seule entrée vocale peut être acquise par l'utilisateur. Par conséquent, vous devriez initier le suivant onActivityResult de la demande vocale précédente.

Vous devez également briser le boîtier de l'interrupteur pour éviter que tous les boîtiers soient exécutés à chaque fois.