英语词典
2015-06-18 21:11
567 查看
1.目的:
在AutoCompleteTextView组件中输入两个及以上字母时显示以所输入字符串开头的所有单词列表
2. 基本思路:
是在AutoCompleteTextView类的afterTextChanged事件中监视AutoCompleteTextView组件中字符的输入情况,每当输入一个字符时就生成一个Adapter对象,然后将新生成的Adapter对象与AutoCompleteTextView关联。效果图如下:
3.步骤:
(1)将dictionary.db文件复制到Eclipse Android工程中的res\raw目录中
(2)使用SQLiteDatabase.openOrCreateDatabase方法来打开任意目录中的SQLite数据库文件。
4.布局:
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:background="@drawable/mainbg"
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=".MainActivity" >
<AutoCompleteTextView
android:id="@+id/etWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="31dp"
android:background="@android:drawable/edit_text"
android:ems="10"
android:hint="@string/searchHint"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
</AutoCompleteTextView>
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/etWord"
android:layout_alignBottom="@+id/etWord"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/etWord"
android:background="@drawable/ibsearchword"
android:onClick="searchWord"
android:text="@string/serachWord" />
<TextView
android:id="@+id/tvSearchResult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/etWord"
android:layout_below="@+id/etWord"
android:layout_marginTop="22dp"
android:textSize="25sp"
android:background="@drawable/bg_roundcorner"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
word_list_item.xml布局
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvWordItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/gray" />
5.java代码:
package com.supermario.dict; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.CursorAdapter; import android.widget.TextView; public class Dictionary extends Activity implements OnClickListener, TextWatcher { //定义数据库的存放路径 private final String DATABASE_PATH = android.os.Environment .getExternalStorageDirectory().getAbsolutePath() + "/dictionary"; //用户输入文本框 private AutoCompleteTextView word; //定义数据库的名字 private final String DATABASE_FILENAME = "dictionary.db"; private SQLiteDatabase database; //搜索按钮 private Button searchWord; //用户显示查询结果 private TextView showResult; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //打开数据库 database = openDatabase(); searchWord = (Button) findViewById(R.id.btnSearch); word = (AutoCompleteTextView) findViewById(R.id.etWord); //绑定监听器 searchWord.setOnClickListener(this); //绑定文字改变监听器 word.addTextChangedListener(this); showResult=(TextView)findViewById(R.id.tvSearchResult); } //自定义Adapter类 public class DictionaryAdapter extends CursorAdapter { private LayoutInflater layoutInflater; @Override 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"))); } //绑定选项到列表中 @Override public void bindView(View view, Context context, Cursor cursor) { setView(view, cursor); } //生成新的选项 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = layoutInflater.inflate(R.layout.word_list_item, null); setView(view, cursor); return view; } public DictionaryAdapter(Context context, Cursor c, boolean autoRequery) { super(context, c, autoRequery); layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } } 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() + "%" }); //新建新的Adapter DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,cursor, true); //绑定适配器 word.setAdapter(dictionaryAdapter); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } public void onClick(View view) { //查询指定的单词 String sql = "select chinese from t_words where english=?"; Cursor cursor = database.rawQuery(sql, new String[] {word.getText().toString()}); String result = "未找到该单词."; // 如果查找单词,显示其中文的意思 if (cursor.getCount() > 0) { // 必须使用moveToFirst方法将记录指针移动到第1条记录的位置 cursor.moveToFirst(); result = cursor.getString(cursor.getColumnIndex("chinese")).replace("&", "&"); } //将结果 4000 显示到TextView中 showResult.setText(word.getText()+"\n"+result.toString()); } 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) { } //如果打开出错,则返回null return null; } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories