天天看點

android朗讀英文

<a href="http://blog.51cto.com/attachment/201104/213710742.jpg" target="_blank"></a>

android讀取英文,是非常簡單的,接口為TextToSpeech.OnInitListener,  

  OnClickListener ,實作方法便行。  

package com.smart;  

import java.util.Locale;  

import android.app.Activity;  

import android.content.ComponentName;  

import android.content.Intent;  

import android.os.Bundle;  

import android.speech.tts.TextToSpeech;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

import android.widget.TextView;  

import android.widget.Toast;  

public class ReadText extends Activity implements TextToSpeech.OnInitListener,  

  OnClickListener {  

 private TextToSpeech tts;  

 private TextView textView;  

 private Button return_back;  

 private Button button;  

 @Override 

 public void onCreate(Bundle savedInstanceState) {  

  super.onCreate(savedInstanceState);  

  setContentView(R.layout.read);  

  tts = new TextToSpeech(this, this);  

  button = (Button) findViewById(R.id.button);  

  textView = (TextView) findViewById(R.id.textView);  

  button.setOnClickListener(this);  

  return_back = (Button) findViewById(R.id.back);  

  return_back.setOnClickListener(new OnClickListener() {  

   @Override 

   public void onClick(View v) {  

    Intent intent=new Intent();  

//    intent.setClass(ReadText.this,Main.class);  

    intent.setComponent(new ComponentName(ReadText.this,Main.class));  

   }  

  });  

 }  

 //執行個體化  

 public void onInit(int status) {  

  if (status == TextToSpeech.SUCCESS) {  

   int result = tts.setLanguage(Locale.US);  

   if (result == TextToSpeech.LANG_NOT_SUPPORTED  

     || result == TextToSpeech.LANG_MISSING_DATA) {  

    Toast.makeText(this, "Language is not available.",  

      Toast.LENGTH_LONG).show();  

  }  

 public void onClick(View v) {  

  //得到内容,然後進行讀取  

  tts.speak(textView.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);  

}  

&lt;?xml version="1.0" encoding="utf-8"?&gt;  

&lt;LinearLayout  

  xmlns:android="http://schemas.android.com/apk/res/android" 

  android:orientation="vertical" 

  android:layout_width="fill_parent" 

  android:layout_height="fill_parent"&gt;  

  &lt;Button  

  android:id="@+id/button" 

  android:layout_height="wrap_content" 

  android:text="讀取内容" 

  /&gt;  

  android:id="@+id/back" 

  android:text="傳回" 

  &lt;TextView  

  android:id="@+id/textView" 

    android:layout_width="fill_parent" 

  android:layout_height="fill_parent" 

  android:text="@string/text" 

&lt;/LinearLayout&gt;  

本文轉自 llb988 51CTO部落格,原文連結:http://blog.51cto.com/llb988/536034,如需轉載請自行聯系原作者

繼續閱讀