參考"在Android應用程序內使用Google Translate API, google-api-translate-java", 下載並添加jar, 和在AndroidMainfest.xml文件中添加"android.permission.INTERNET"權限.
修改main.xml, 多添加一個英語發聲按鍵
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My English to Chinese Translate"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/texttotranlate"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text = "英語發聲"
android:id="@+id/tospeech"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "Translate"
android:id="@+id/totranslate"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/comment"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/translatedtext"
/>
</LinearLayout>
修改主代碼.
package com.AndroidTranslate;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class AndroidTranslate extends Activity implements OnInitListener{
EditText textToTranlate;
Button buttonToTranslate;
Button buttonToSpeech;
TextView textComment;
TextView translatedText;
final int MY_TTS_CHECK_CODE=1;
private TextToSpeech myTTS;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textToTranlate = (EditText)findViewById(R.id.texttotranlate);
buttonToTranslate = (Button)findViewById(R.id.totranslate);
buttonToSpeech = (Button)findViewById(R.id.tospeech);
textComment = (TextView)findViewById(R.id.comment);
translatedText = (TextView)findViewById(R.id.translatedtext);
buttonToTranslate.setOnClickListener(buttonToTranslateOnClickListener);
buttonToSpeech.setOnClickListener(buttonToSpeechOnClickListener);
//Check if TextToSpeech Engine installed.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_TTS_CHECK_CODE);
}
private Button.OnClickListener buttonToTranslateOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String strTranslatedText = null;
String strTextToTranlate = textToTranlate.getText().toString();
Translate.setHttpReferrer("http://androidbiancheng.blogspot.com/");
try {
strTranslatedText = Translate.execute(strTextToTranlate, Language.ENGLISH, Language.CHINESE_TRADITIONAL);
textComment.setText("Text Translated:");
translatedText.setText(strTranslatedText);
} catch (Exception e) {
// TODO Auto-generated catch block
textComment.setText("--- Error in Translate ---");
translatedText.setText(null);
e.printStackTrace();
}
}};
private Button.OnClickListener buttonToSpeechOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String textToSpeech = textToTranlate.getText().toString();
myTTS.setLanguage(Locale.ENGLISH);
myTTS.speak(textToSpeech, TextToSpeech.QUEUE_ADD, null);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == MY_TTS_CHECK_CODE){
if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
//TextToSpeech Engine installed.
myTTS = new TextToSpeech(this, this);
}else{
//TextToSpeech Engine NOT installed, request to install.
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
//Check if Language available
switch(myTTS.isLanguageAvailable(Locale.ENGLISH)){
case(TextToSpeech.LANG_AVAILABLE):
buttonToSpeech.setEnabled(true);
break;
case(TextToSpeech.LANG_COUNTRY_AVAILABLE):
buttonToSpeech.setEnabled(true);
break;
case(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE):
buttonToSpeech.setEnabled(true);
break;
case(TextToSpeech.LANG_MISSING_DATA):
buttonToSpeech.setEnabled(false);
break;
case(TextToSpeech.LANG_NOT_SUPPORTED):
buttonToSpeech.setEnabled(false);
break;
default:
buttonToSpeech.setEnabled(false);
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myTTS.shutdown();
}
}
沒有留言:
發佈留言