我的作业是使用Spinner选择语言的文字转语音应用。 但我对这个话题不太了解。
这是我的MainActivity.java代码:
package com.detainhom6.doctext;
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.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity implements OnInitListener{
// Khai bao cac bien can thiet
String chuoinhap;
String ngonngu[] = {"English", "Japan", "Korea", "China"};
EditText txtNhap;
Button btnNoi;
Toast note;
Spinner spnn;
//Khai bao bien text to speech
private TextToSpeech t2p;
private int kiemtradulieu = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// gan gia tri cho bien vua khai bao
txtNhap = (EditText)findViewById(R.id.txtnhap);
btnNoi = (Button)findViewById(R.id.btnNoi);
spnn = (Spinner)findViewById(R.id.menungonngu);
//
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ngonngu);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spnn.setAdapter(adapter);
spnn.setOnItemSelectedListener(new ClickChonNgonNgu());
Intent kiemtrat2pintent = new Intent();
kiemtrat2pintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(kiemtrat2pintent, kiemtradulieu);
btnNoi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View mangV) {
// TODO Auto-generated method stub
chuoinhap = txtNhap.getText().toString();
doc(chuoinhap);
}
});
}
// Tao Ham khi Goi Intent Acivity Result
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == kiemtradulieu){
if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
t2p = new TextToSpeech(this,this);
}
else
{
Intent caidatt2pIntent = new Intent();
caidatt2pIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(caidatt2pIntent);
}
}
}
// Khoi Tao Ham Doc Chuoi Da Nhap
protected void doc(String chuoinhap2) {
// TODO Auto-generated method stub
t2p.speak(chuoinhap2, TextToSpeech.QUEUE_FLUSH, null);
}
// Tao su kien khi chon item trong Spinner
public class ClickChonNgonNgu implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String nndachon;
nndachon = ngonngu[position];
if(nndachon == "English"){
t2p.setLanguage(Locale.US);
note = Toast.makeText(MainActivity.this, "Bạn Dã Chọn English", Toast.LENGTH_SHORT);
note.setGravity(Gravity.CENTER, 0, 0);
note.show();
}
if(nndachon == "Japan"){
t2p.setLanguage(Locale.JAPAN);
note = Toast.makeText(MainActivity.this, "Bạn đã chọn Japan", Toast.LENGTH_SHORT);
note.setGravity(Gravity.CENTER, 0, 0);
note.show();
}
if(nndachon == "Korea"){
t2p.setLanguage(Locale.KOREA);
note = Toast.makeText(MainActivity.this, "Bạn đã chọn Korea", Toast.LENGTH_SHORT);
note.setGravity(Gravity.CENTER, 0, 0);
note.show();
}
if(nndachon == "China"){
t2p.setLanguage(Locale.CHINA);
note = Toast.makeText(MainActivity.this, "Bạn đã chọn China", Toast.LENGTH_SHORT);
note.setGravity(Gravity.CENTER, 0, 0);
note.show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status == TextToSpeech.SUCCESS)
{
t2p.setLanguage(Locale.US);
Log.e("TTS INIT", "TTS Thành Công");
}
else if(status == TextToSpeech.ERROR)
{
Log.e("TTS INIT", "TTS Lỗi");
}
}
}
这是activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.detainhom6.doctext.MainActivity" >
<TextView
android:id="@+id/txtnote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/note1" />
<Button
android:id="@+id/btnNoi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtnhap"
android:layout_below="@+id/txtnhap"
android:layout_marginTop="21dp"
android:text="@string/Button_Noi" />
<EditText
android:id="@+id/txtnhap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtnote"
android:layout_below="@+id/txtnote"
android:layout_marginTop="116dp"
android:ems="10"
android:hint="@string/hint_txt" >
<requestFocus />
</EditText>
<Spinner
android:id="@+id/menungonngu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtnhap"
android:layout_below="@+id/txtnote"
android:layout_marginTop="34dp" />
</RelativeLayout>
如果可以,有人可以帮我修改代码吗? 非常感谢!