原文链接:http://www.javaarch.net/jiagoushi/782.htm

Android 语音输入API使用Android已经支持语音输入的API了,不过不知道中文输入识别效果怎么样。这里给一个怎么使用语音输入的示例首先在android工程中的页面布局文件中res/layout/main.xml添加一个button和text<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_above="@+id/textView1"android:layout_toLeftOf="@+id/textView1"android:gravity="center"android:orientation="vertical" > <ImageButtonandroid:id="@+id/btnSpeak"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:layout_marginRight="10dp"android:layout_marginTop="10dp"android:contentDescription="@string/speak"android:src="@android:drawable/ic_btn_speak_now" /> <TextViewandroid:id="@+id/txtText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="10dp"android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>active类SpeechToTextDemoActivity.javapackage net.viralpatel.android.speechtotextdemo; import java.util.ArrayList; import android.app.Activity;import android.content.ActivityNotFoundException;import android.content.Intent;import android.os.Bundle;import android.speech.RecognizerIntent;import android.view.Menu;import android.view.View;import android.widget.ImageButton;import android.widget.TextView;import android.widget.Toast; public class MainActivity extends Activity { protected static final int RESULT_SPEECH = 1; private ImageButton btnSpeak;private TextView txtText; @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); txtText = (TextView) findViewById(R.id.txtText); btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); btnSpeak.setOnClickListener(new View.OnClickListener() { @Overridepublic void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); try {startActivityForResult(intent, RESULT_SPEECH);txtText.setText("");} catch (ActivityNotFoundException a) {Toast t = Toast.makeText(getApplicationContext(),"Opps! Your device doesn't support Speech to Text",Toast.LENGTH_SHORT);t.show();}}}); } @Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;} @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data); switch (requestCode) {case RESULT_SPEECH: {if (resultCode == RESULT_OK && null != data) { ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); txtText.setText(text.get(0));}break;} }}}这里android.speech是Android语音输入的核心包,android.speech.RecognizerIntent是一个主要的类,这个active会弹出一个语音输入对话框,然后接收语音输入,识别语音内容转为文本,但我们启动语音输入active后,需要通过startActivityForResult()方法接收文本结果。在.putExtra()方法还需要输入RecognizerIntent.EXTRA_LANGUAGE_MODE语言类型,这里是en-US。我们通过覆盖onActivityResult(int requestCode, int resultCode, Intent data)方法来处理结果数据,通过data获取key为RecognizerIntent.EXTRA_RESULTS来接收文本内容list,然后设置到text框上。示例代码地址:http://viralpatel-net-tutorials.googlecode.com/files/SpeechToTextDemo.zip

更多相关文章

  1. Android团队提供的示例项目
  2. 玩转Android---UI篇---TextView(文本框)
  3. 解决android TextView多行文本(超过3行)使用ellipsize属性无效问题
  4. Android下载文本文件和mp3文件
  5. Android中ProgressDialog的简单示例
  6. android从网上加载图片简单示例

随机推荐

  1. [Android]如何做一个崩溃率少于千分之三
  2. Android(安卓)基于Socket的聊天应用实例(
  3. android中软键盘enter键更改图标
  4. 移动端开发工程师的AI突围之路
  5. Android它Service
  6. Android(安卓)API Guiede---Animation an
  7. Android下音标字体的相关问题
  8. android Task类
  9. android批量文件上传(android批量图片上传
  10. Android(安卓)图片内存溢出(Out of Memory