android朗讀英文

 

 

 

 

  1. android讀取英文,是非常簡單的,接口爲TextToSpeech.OnInitListener,  
  2.   OnClickListener ,實現方法便行。  
  3.  
  4. package com.smart;  
  5.  
  6. import java.util.Locale;  
  7.  
  8. import android.app.Activity;  
  9. import android.content.ComponentName;  
  10. import android.content.Intent;  
  11. import android.os.Bundle;  
  12. import android.speech.tts.TextToSpeech;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18.  
  19. public class ReadText extends Activity implements TextToSpeech.OnInitListener,  
  20.   OnClickListener {  
  21.  private TextToSpeech tts;  
  22.  private TextView textView;  
  23.  private Button return_back;  
  24.  private Button button;  
  25.  
  26.  @Override 
  27.  public void onCreate(Bundle savedInstanceState) {  
  28.   super.onCreate(savedInstanceState);  
  29.   setContentView(R.layout.read);  
  30.   tts = new TextToSpeech(thisthis);  
  31.   button = (Button) findViewById(R.id.button);  
  32.   textView = (TextView) findViewById(R.id.textView);  
  33.   button.setOnClickListener(this);  
  34.   return_back = (Button) findViewById(R.id.back);  
  35.  
  36.   return_back.setOnClickListener(new OnClickListener() {  
  37.      
  38.    @Override 
  39.    public void onClick(View v) {  
  40.     Intent intent=new Intent();  
  41. //    intent.setClass(ReadText.this,Main.class);  
  42.     intent.setComponent(new ComponentName(ReadText.this,Main.class));  
  43.    }  
  44.   });  
  45.  
  46.  }  
  47.  //實例化  
  48.  @Override 
  49.  public void onInit(int status) {  
  50.   if (status == TextToSpeech.SUCCESS) {  
  51.    int result = tts.setLanguage(Locale.US);  
  52.    if (result == TextToSpeech.LANG_NOT_SUPPORTED  
  53.      || result == TextToSpeech.LANG_MISSING_DATA) {  
  54.     Toast.makeText(this"Language is not available.",  
  55.       Toast.LENGTH_LONG).show();  
  56.    }  
  57.   }  
  58.  }  
  59.  
  60.  @Override 
  61.  public void onClick(View v) {  
  62.   //得到內容,然後進行讀取  
  63.   tts.speak(textView.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);  
  64.     
  65.     
  66.  
  67.  }  
  68.  
  69. }  
  70. <?xml version="1.0" encoding="utf-8"?>  
  71. <LinearLayout  
  72.   xmlns:android="http://schemas.android.com/apk/res/android" 
  73.   android:orientation="vertical" 
  74.   android:layout_width="fill_parent" 
  75.   android:layout_height="fill_parent">  
  76.     
  77.     
  78.     
  79.     
  80.   <Button  
  81.   android:id="@+id/button" 
  82.   android:layout_width="fill_parent" 
  83.   android:layout_height="wrap_content" 
  84.   android:text="讀取內容" 
  85.   />  
  86.       
  87.   <Button  
  88.   android:id="@+id/back" 
  89.   android:layout_width="fill_parent" 
  90.   android:layout_height="wrap_content" 
  91.   android:text="返回" 
  92.   />  
  93.     
  94.   <TextView  
  95.   android:id="@+id/textView" 
  96.     android:layout_width="fill_parent" 
  97.   android:layout_height="fill_parent" 
  98.   android:text="@string/text" 
  99.     
  100.   />  
  101. </LinearLayout>  
  102.  
  103.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章