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 theSpeech Recognition
is activated?– Ivan Silva