Set voice value in an editText

Asked

Viewed 49 times

1

When I press the button I speak the name for example might it put the autofocus in editText, but when it is with Focus in edittext should press the button and say the value to be written in that field, someone help me please.

this is mainActivity.java

private final int REQ_CODE_SPECH_INPUT = 100;
EditText potencia;
EditText tensao;
EditText corrente;
Button calcular;
View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
private void promptSpeechInput(){
    Intent it = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    it.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    it.putExtra(RecognizerIntent.EXTRA_PROMPT, "Diga Algo");
    try{
        startActivityForResult(it,REQ_CODE_SPECH_INPUT);
    }catch (ActivityNotFoundException a){
    }
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    switch (requestCode) {
        case REQ_CODE_SPECH_INPUT: {
        if(resultCode == RESULT_OK && data != null){
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                String mensagem = result.get(0);
                potencia = findViewById(R.id.potencia);
                tensao = findViewById(R.id.tensao);
                corrente = findViewById(R.id.corrente);
                calcular = findViewById(R.id.calcular);
                v = findViewById(R.id.view);
                if(mensagem.equals("potência")){
                    potencia.requestFocus();
                }else
                if(mensagem.equals("tensão")){
                    tensao.requestFocus();
                }else
                if(mensagem.equals("corrente")){
                    corrente.requestFocus();
                }else
                if(mensagem.equals("calcular")){
                }
            }
        }
    }
}
public void Falar(View view) {
    promptSpeechInput();
}

}

this is activit.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="79dp"
        android:layout_height="wrap_content"

        android:text="Potência" />

    <EditText
        android:id="@+id/potencia"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="79dp"
        android:layout_height="wrap_content"

        android:text="Tensão" />

    <EditText
        android:id="@+id/tensao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="79dp"
        android:layout_height="wrap_content"

        android:text="Corrente" />

    <EditText
        android:id="@+id/corrente"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="79dp"
        android:layout_height="wrap_content"

        android:text="Resistência" />

    <EditText
        android:id="@+id/resistencia"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>

<Button
    android:id="@+id/calcular"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Microfone"
    android:onClick="Falar"
    />

  • It wasn’t very clear to me, but you wish that when the EditText gain focus on the Speech Recognition is activated?

1 answer

0

This section calls onActivityResult() startActivityForResult(it,REQ_CODE_SPECH_INPUT);

I commented on the lines that were added and explained their logic within the code.

private final int REQ_CODE_SPECH_INPUT = 100;
private String textViewFocus = ""; //Adicionei essa linha pra voce saber quem esta com Focus quando chamar novamente o onActivityResult()
EditText potencia;
EditText tensao;
EditText corrente;
Button calcular;
View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
private void promptSpeechInput(){
    Intent it = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    it.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    it.putExtra(RecognizerIntent.EXTRA_PROMPT, "Diga Algo");
    try{
        startActivityForResult(it,REQ_CODE_SPECH_INPUT);
    }catch (ActivityNotFoundException a){
    }
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    switch (requestCode) {
        case REQ_CODE_SPECH_INPUT: {
        if(resultCode == RESULT_OK && data != null)  {
                ArrayList<String> result = 
                data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                String mensagem = result.get(0);
                potencia = findViewById(R.id.potencia);
                tensao = findViewById(R.id.tensao);
                corrente = findViewById(R.id.corrente);
                calcular = findViewById(R.id.calcular);
                v = findViewById(R.id.view);
                if(mensagem.equals("potência")){
                    potencia.requestFocus();
                    if(potencia.hasFocus()) {
                        promptSpeechInput()
                        textViewFocus = "potencia";
                    } //Caso esteja com Focus, chama o metodo de Voz novamente.    
                }
                else if(mensagem.equals("tensão")){
                    tensao.requestFocus();
                    if(mensagem.hasFocus()) {
                        promptSpeechInput()
                        textViewFocus = "tensão";
                    }
                }
                else if(mensagem.equals("corrente")){
                    corrente.requestFocus();
                    if(corrente.hasFocus()) {
                        promptSpeechInput()
                        textViewFocus = "corrente";
                    }
                }
                else if(mensagem.equals("calcular")){
                    if(calcular.hasFocus()) {
                        promptSpeechInput()
                        textViewFocus = "calcular";
                    }
                }else {
                    //Essa ultima condicao vai verificar se algo diferente dos textos acima foi dito e entao inserir no textView q tem Focus True
                    switch (textViewFocus) {
                        case "potencia" :
                        potencia.setText(mensagem); // Fiz so o exemplo de potencia pra voce testar, caso de certo e so replicar
                        break;
                    }
                }

                }

            }
        }
    }
}
public void Falar(View view) {
    promptSpeechInput();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.