该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package com.example.dictionary;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import com.iflytek.speech.RecognizerResult;
import com.iflytek.speech.SpeechError;
import com.iflytek.ui.RecognizerDialog;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class OfflineQuery extends Activity implements OnClickListener,
TextWatcher{
public static final int MENU_ABOUT = 1;
public static final int MENU_TUICHU = MENU_ABOUT + 2;
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath() + "/dictionary";
private AutoCompleteTextView actvWord;
private final String DATABASE_FILENAME = "dictionary.db";
private SQLiteDatabase database;
private Button btnSelectWord;
private Button Clear;
private TextView mTextView;
private String result;
private ArrayList> arrayList;
TextToSpeech tts;
ImageButton speech;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offline_query);
tts = new TextToSpeech(this, new OnInitListener()
{
@Override
public void onInit(int status)
{
// 如果装载TTS引擎成功
if (status == TextToSpeech.SUCCESS)
{
// 设置使用美式英语朗读
int result = tts.setLanguage(Locale.US);
// 如果不支持所设置的语言
if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE)
{
Toast.makeText(OfflineQuery.this
, "TTS暂时不支持这种语言的朗读。", Toast.LENGTH_LONG)
.show();
}
}
}
});
speech = (ImageButton) findViewById(R.id.image_btn_dict_horn_accent_eng);
speech.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 执行朗读
tts.speak(actvWord.getText().toString(),
TextToSpeech.QUEUE_ADD, null);
}
});
database = openDatabase();
btnSelectWord = (Button) findViewById(R.id.ButtonGo);
actvWord = (AutoCompleteTextView) findViewById(R.id.seek);
Clear = (Button) findViewById(R.id.clearButton);
btnSelectWord.setOnClickListener(this);
actvWord.addTextChangedListener(this);
Clear.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
actvWord.setText("");
}
});
}
public void onDestroy()
{
// 关闭TextToSpeech对象
if (tts != null)
{
tts.shutdown();
}
}
public class DictionaryAdapter extends CursorAdapter {
private LayoutInflater layoutInflater;
public CharSequence convertToString(Cursor cursor) {
return cursor == null ? "" : cursor.getString(cursor
.getColumnIndex("_id"));
}
private void setView(View view, Cursor cursor) {
TextView tvWordItem = (TextView) view;
tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));
tvWordItem.setPadding(15, 10, 10, 15);
tvWordItem.setTextSize(18);
}
public DictionaryAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
// TODO Auto-generated constructor stub
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
setView(view, cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
View view = new TextView(OfflineQuery.this);
setView(view, cursor);
return view;
}
}
private SQLiteDatabase openDatabase() {
try {
// 获得dictionary.db文件的绝对路径
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
// 如果/sdcard/dictionary目录中存在,创建这个目录
if (!dir.exists())
dir.mkdir();
// 如果在/sdcard/dictionary目录中不存在
// dictionary.db文件,则从res\raw目录中复制这个文件到
// SD卡的目录(/sdcard/dictionary)
if (!(new File(databaseFilename)).exists()) {
// 获得封装dictionary.db文件的InputStream对象
InputStream is = getResources().openRawResource(
R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
// 开始复制dictionary.db文件
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
// 打开/sdcard/dictionary目录中的dictionary.db文件
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
} catch (Exception e) {
}
return null;
}
public void afterTextChanged(Editable s) {
// 必须将english字段的别名设为_id
Cursor cursor = database.rawQuery(
"select english as _id from t_words where english like ?",
new String[] { s.toString() + "%" });
DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,
cursor, true);
actvWord.setAdapter(dictionaryAdapter);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void onClick(View view) {
// TODO Auto-generated method stub
String sql = "select chinese from t_words where english=?";
Cursor cursor = database.rawQuery(sql, new String[] { actvWord
.getText().toString() });
result = "未找到该单词.";
// 如果查找单词,显示其中文的意思
if (cursor.getCount() > 0) {
// 必须使用moveToFirst方法将记录指针移动到第1条记录的位置
cursor.moveToFirst();
result = cursor.getString(cursor.getColumnIndex("chinese"));
}
if (actvWord.getText().toString().equals("")) {
Toast.makeText(OfflineQuery.this, "查询内容不能为空!", Toast.LENGTH_LONG)
.show();
return;
}
TextView tv=(TextView) findViewById(R.id.textView2);
tv.setText(actvWord.getText().toString());
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setText(" "+result);
SimpleAdapter adapter = new SimpleAdapter(this, arrayList,
android.R.layout.simple_list_item_2, new String[] { "word",
"explain" }, new int[] { android.R.id.text1,
android.R.id.text2 });
}
}
——————————————————————————————————————————————
点击发音按钮,没有声音 大神帮忙看看
logcat显示的问题
04-14 11:33:10.392: W/TextToSpeech(2947): speak failed: not bound to TTS engine