【问题标题】:Text doesn't get printed on editText after Voice Recognition in android在android中进行语音识别后,文本不会打印在editText上
【发布时间】:2017-06-15 22:58:16
【问题描述】:

在运行时,语音识别 Intent 会启动,但不会在 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>

【问题讨论】:

    标签: java android eclipse android-activity voice-recognition


    【解决方案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);
    
    }
    

    【讨论】:

    • 代码中的一切看起来都很好,可能是您的语音无法识别,请尝试将您的设备连接到互联网,另请参阅在清单中添加互联网权限
    • 删除i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 行并查看,同时打印您的结果列表,看看您是否收到任何字符串
    【解决方案2】:

    使用这个官方教程,我将它用于操作栏。在创建可搜索和元数据时要格外小心。 https://developer.android.com/training/search/setup.html

    如果您想要同一活动中的文本结果,请将Search Intent Filter 指向您当前的活动并将该活动设为singleInstancesingleTop

    【讨论】:

    • 谢谢...会调查的!
    • 这是另一种方式
    【解决方案3】:

    嘿,我为这样的错误道歉..但意识到代码是正确的,但我在 xml 文件中为表格布局设置了背景白色,因此无法意识到它已在编辑时打印文字....当然是一个罕见的错误...希望它可以帮助那里的人.. 上面的代码适用于语音识别...从 xml 中排除以下部分:

    background:"@colors/white"
    

    【讨论】:

      【解决方案4】:

      您需要获取从活动中获得的结果并将 EditText 的文本设置为等于它。

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

      【讨论】:

      猜你喜欢
      • 2020-12-31
      • 1970-01-01
      • 2017-02-07
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多