2017-01-30 2 views
0

При управлении, цель начинается распознавание речи, но не печатает ничего на к EditTextТекста не получает напечатанные на EditText после распознавания голоса в андроиде

Ниже приведен код:

import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.speech.RecognizerIntent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Locale; 

public class dialog1cl extends AppCompatActivity { 
public EditText ed; 
    private static final int av= 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.dialog1); 
     ed=(EditText)findViewById(R.id.editText); 
     ImageButton im=(ImageButton)findViewById(R.id.imageButton); 
     im.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View view){ 

       promptspeech(); 
      } 
     }); 

    } 
    public void promptspeech() 
    { 
     Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
     i.putExtra(RecognizerIntent.EXTRA_PROMPT,"Say Something!"); 

     try { 
      startActivityForResult(i,av); 
     } 
     catch(ActivityNotFoundException a) 
     { 
      Toast.makeText(dialog1cl.this,"Sorry! Your device doesnt support speech Language",Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void onActivityResult(int request_code , int result_code, Intent i){ 



      if(request_code ==av && result_code==RESULT_OK) 
      { 
       List<String> result=i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
ed.setText(result.get(0)); 
      } 
     super.onActivityResult(request_code, result_code, i); 

    } 
} 

код XML:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:weightSum="1" 

    android:id="@+id/lin1"> 
    <ImageView 

     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:scaleType="center" 
     android:background="#FFFFBB32" 
     android:contentDescription="PARAMETERS" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="34dp" 
     android:textSize="20dp" 
     android:textStyle="bold" 
     android:textColor = "@color/blue1" 
     android:text="Task Name?" 
     android:id="@+id/textView2" 
     android:layout_gravity="center_horizontal" /> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <EditText 
      android:layout_width="250dp" 
      android:layout_height="wrap_content" 
      android:inputType="textPersonName" 
      android:hint="Enter your Task" 
      android:ems="10" 
      android:id="@+id/editText" /> 

     <ImageButton 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:background="@color/white" 
      android:id="@+id/imageButton" 
      android:src="@drawable/ic_action_voice" 
      android:layout_column="22" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="match_parent" 
      android:layout_height="34dp" 
      android:textSize="20dp" 
      android:textStyle="bold" 
      android:textColor = "@color/blue1" 
      android:text="DeadLine?" 
      android:id="@+id/textView3" 
      android:layout_column="0" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <EditText 
      android:inputType="textPersonName" 
      android:hint="Name" 
      android:ems="10" 
      android:id="@+id/editText2" 
      android:layout_column="0" 
      android:focusable="false"/> 

     <ImageButton 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:src="@drawable/ic_action_calendar" 
      android:background="@color/white" 
      android:id="@+id/imageButton2" 

      android:layout_column="22" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 

      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="match_parent" 
      android:layout_height="34dp" 
      android:textSize="20dp" 
      android:textStyle="bold" 
      android:textColor = "@color/blue1" 
      android:text="Time?" 
      android:id="@+id/textView4" 
      android:layout_column="0" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <EditText 
      android:inputType="textPersonName" 
      android:text="Name" 
      android:ems="10" 
      android:id="@+id/editText3" 
      android:layout_column="0" /> 

     <ImageButton 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:src="@drawable/ic_action_time" 
      android:background="@color/white" 
      android:id="@+id/imageButton3" 

      android:layout_column="22" /> 
    </TableRow> 


</TableLayout> 

ответ

0

Привет, я извиняюсь за такую ​​ошибку. Но понял, что код был прав, но я сделал свой белый фон в XML-файле для макета таблицы, поэтому не смог понять, что он печатал на редактировать текст .... Ofcourse редкая ошибка ... надеюсь, что это помогает кто-то там .. Приведенный выше код отлично подходит для распознавания голоса работает ... исключить ниже часть из XML:

background:"@colors/white" 
0

вы должны принять тот результат, который вы получите от деятельности и установить текст в EditText к равным его.

List<String> result=i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
String text = getTextFromResult(result); 
ed.setText(text); 
+0

нет getTextFromResult. – nishchalpro

+1

Вам придется написать это самостоятельно. Вам нужно преобразовать список в строку, которую вы хотите поместить в EditText. –

+0

. Как конвертировать брокера. – nishchalpro

1

изменить вас о результатах деятельности с этим, вы должны установить программно текст на редактирования текста

public void onActivityResult(int request_code , int result_code, Intent i){ 
     if(request_code ==av && result_code==RESULT_OK) 
     { 
      List<String> result=i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

       ed.setText(result.get(0)); 
     } 
    super.onActivityResult(request_code, result_code, i); 

} 
+0

Его то же самое..не работает! – nishchalpro

+0

всякая вещь прекрасно выглядит в коде, может быть, ваша речь не распознана, попробуйте подключить свое устройство к Интернету и посмотрите, также добавьте разрешение на интернет в манифесте – Redman

+0

tat была хорошей информацией, но никаких улучшений ... – nishchalpro

1

Используя этот официальный учебник я получил это работает для ActionBar. Будьте осторожны при создании поисковых и метаданных. https://developer.android.com/training/search/setup.html

Если вы хотите, чтобы результаты этого текста в одной и той же деятельности, указать Search Intent Filter к текущей деятельности и сделать эту деятельность singleInstance или singleTop.

+0

Спасибо ... заглянет в него! – nishchalpro

+0

это другой способ сделать это – nishchalpro

Смежные вопросы