AutoComplectView英语词典

学习完AutoComplectView,写了一个英语词典的小程序

效果图如下:



activity_main.xml

[html]  view plain copy
  1. <span style="font-family:Comic Sans MS;font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/mainbg"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity" >  
  11.   
  12.     <AutoCompleteTextView  
  13.         android:id="@+id/etWord"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_alignParentLeft="true"  
  17.         android:layout_alignParentTop="true"  
  18.         android:layout_marginTop="31dp"  
  19.         android:background="@android:drawable/edit_text"  
  20.         android:ems="10"  
  21.         android:hint="@string/searchHint"  
  22.         android:singleLine="true"  
  23.         android:textColor="#552006"  
  24.         android:textColorHint="#782f10" >  
  25.   
  26.     </AutoCompleteTextView>  
  27.   
  28.     <Button  
  29.         android:id="@+id/btnSearch"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_alignBaseline="@+id/etWord"  
  33.         android:layout_alignBottom="@+id/etWord"  
  34.         android:layout_marginLeft="16dp"  
  35.         android:layout_toRightOf="@+id/etWord"  
  36.         android:background="@drawable/ibsearchword"  
  37.         android:onClick="searchWord"  
  38.         android:text="@string/serachWord" />  
  39.   
  40.     <TextView  
  41.         android:id="@+id/tvSearchResult"  
  42.         android:layout_width="match_parent"  
  43.         android:layout_height="match_parent"  
  44.         android:layout_alignLeft="@+id/etWord"  
  45.         android:layout_below="@+id/etWord"  
  46.         android:layout_marginTop="22dp"  
  47.         android:textSize="25sp"  
  48.         android:background="@drawable/bg_roundcorner"  
  49.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  50.   
  51. </RelativeLayout></span>  

word_list_item.xml

[html]  view plain copy
  1. <span style="font-family:Comic Sans MS;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/tvWordItem"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:gravity="center_vertical"  
  7.     android:minHeight="?android:attr/listPreferredItemHeight"  
  8.     android:paddingLeft="6dip"  
  9.     android:textAppearance="?android:attr/textAppearanceLarge"  
  10.     android:textColor="@color/gray" />  
  11.   
  12. </span>  

Dictionary.java

[html]  view plain copy
  1. <span style="font-family:Comic Sans MS;font-size:14px;">package com.bzu.gxs;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6.   
  7. import com.bzu.gxs.dao.DictionaryAdapter;  
  8. import com.bzu.gxs.db.DBHelper;  
  9. import com.bzu.gxs.R;  
  10.   
  11. import android.app.Activity;  
  12. import android.content.Context;  
  13. import android.database.Cursor;  
  14. import android.database.sqlite.SQLiteDatabase;  
  15. import android.os.Bundle;  
  16. import android.text.Editable;  
  17. import android.text.TextWatcher;  
  18. import android.util.Log;  
  19. import android.view.LayoutInflater;  
  20. import android.view.View;  
  21. import android.view.ViewGroup;  
  22. import android.view.View.OnClickListener;  
  23. import android.widget.AutoCompleteTextView;  
  24. import android.widget.Button;  
  25. import android.widget.CursorAdapter;  
  26. import android.widget.TextView;  
  27.   
  28. public class Dictionary extends Activity implements OnClickListener,  
  29.         TextWatcher {  
  30.     private DBHelper dbHelper; // 用户输入文本框  
  31.     private AutoCompleteTextView word; // 定义数据库的名字  
  32.     private SQLiteDatabase database;  
  33.     private Button searchWord; // 搜索按钮  
  34.     private TextView showResult; // 用户显示查询结果  
  35.   
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.activity_main);  
  40.   
  41.         dbHelper = new DBHelper(getBaseContext());// 打开数据库  
  42.         database = dbHelper.openDatabase();  
  43.   
  44.         init();  
  45.   
  46.         searchWord.setOnClickListener(this); // 绑定监听器  
  47.         word.addTextChangedListener(this); // 绑定文字改变监听器  
  48.   
  49.     }  
  50.   
  51.     public void init() {  
  52.         searchWord = (Button) findViewById(R.id.btnSearch);  
  53.         word = (AutoCompleteTextView) findViewById(R.id.etWord);  
  54.         showResult = (TextView) findViewById(R.id.tvSearchResult);  
  55.   
  56.     }  
  57.   
  58.     public void afterTextChanged(Editable s) {  
  59.         Cursor cursor = database.rawQuery(  
  60.                 "select english as _id from t_words where english like ?",  
  61.                 new String[] { s.toString() + "%" });  
  62.           
  63.         // 新建新的Adapter  
  64.         DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,cursor, true);  
  65.           
  66.         // 绑定适配器  
  67.         word.setAdapter(dictionaryAdapter);  
  68.   
  69.     }  
  70.   
  71.     public void beforeTextChanged(CharSequence s, int start, int count,  
  72.             int after) {  
  73.   
  74.     }  
  75.   
  76.     public void onTextChanged(CharSequence s, int start, int before, int count) {  
  77.   
  78.     }  
  79.   
  80.     public void onClick(View view) {  
  81.         // 查询指定的单词  
  82.         String sql = "select chinese from t_words where english=?";  
  83.           
  84.         Cursor cursor = database.rawQuery(sql, new String[] { word.getText()  
  85.                 .toString() });  
  86.           
  87.         String result = "查无该词"; // 如果查找单词,显示其中文的意思  
  88.           
  89.         if (cursor.getCount() > 0) {   
  90.               
  91.             cursor.moveToFirst(); // 须使用moveToFirst方法将记录指针移动到第1条记录的位置  
  92.             result = cursor.getString(cursor.getColumnIndex("chinese"))  
  93.                     .replace("&", "&");  
  94.         }  
  95.   
  96.         showResult.setText(word.getText() + "\n" + result.toString());// 将结果显示到TextView中  
  97.     }  
  98.   
  99. }</span>  

DictionaryAdapter.java

[html]  view plain copy
  1. <span style="font-family:Comic Sans MS;font-size:14px;">package com.bzu.gxs.dao;  
  2.   
  3. import android.content.Context;  
  4. import android.database.Cursor;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.CursorAdapter;  
  9. import android.widget.TextView;  
  10. import com.bzu.gxs.R;  
  11.   
  12. //自定义Adapter类  
  13. public class DictionaryAdapter extends CursorAdapter {  
  14.     private LayoutInflater layoutInflater;  
  15.   
  16.     @Override  
  17.     public CharSequence convertToString(Cursor cursor) {  
  18.         return cursor == null ? "" : cursor.getString(cursor  
  19.                 .getColumnIndex("_id"));  
  20.     }  
  21.   
  22.     // 将单词信息显示到列表中  
  23.     private void setView(View view, Cursor cursor) {  
  24.         TextView tvWordItem = (TextView) view;  
  25.         tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));  
  26.     }  
  27.   
  28.     // 绑定选项到列表中  
  29.     @Override  
  30.     public void bindView(View view, Context context, Cursor cursor) {  
  31.         setView(view, cursor);  
  32.     }  
  33.   
  34.     // 生成新的选项  
  35.     @Override  
  36.     public View newView(Context context, Cursor cursor, ViewGroup parent) {  
  37.         View view = layoutInflater.inflate(R.layout.word_list_item, null);  
  38.         setView(view, cursor);  
  39.         return view;  
  40.     }  
  41.   
  42.     public DictionaryAdapter(Context context, Cursor c, boolean autoRequery) {  
  43.         super(context, c, autoRequery);  
  44.         layoutInflater = (LayoutInflater) context  
  45.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  46.     }  
  47. }</span>  

DBHelper.java

[html]  view plain copy
  1. <span style="font-family:Comic Sans MS;font-size:14px;">package com.bzu.gxs.db;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6. import android.content.Context;  
  7. import android.database.sqlite.SQLiteDatabase;  
  8. import com.bzu.gxs.R;  
  9. public class DBHelper {  
  10.     //定义数据库的存放路径  
  11.         private final String DATABASE_PATH = android.os.Environment  
  12.                 .getExternalStorageDirectory().getAbsolutePath()  
  13.                 + "/dictionary";  
  14.         private final String DATABASE_FILENAME = "dictionary.db";  
  15.           
  16.         private Context context;  
  17.   
  18.         public DBHelper(Context context) {  
  19.             this.context = context;  
  20.         }  
  21.           
  22.           
  23.         public SQLiteDatabase openDatabase()  
  24.         {  
  25.             try  
  26.             {  
  27.                 // 获得dictionary.db文件的绝对路径  
  28.                 String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;  
  29.                 File dir = new File(DATABASE_PATH);                                                                                                           
  30.                 // 如果/sdcard/dictionary目录中存在,创建这个目录  
  31.                 if (!dir.exists())  
  32.                     dir.mkdir();  
  33.                 // 如果在/sdcard/dictionary目录中不存在  
  34.                 // dictionary.db文件,则从res\raw目录中复制这个文件到  
  35.                 // SD卡的目录(/sdcard/dictionary)  
  36.                 if (!(new File(databaseFilename)).exists())  
  37.                 {  
  38.                     // 获得封装dictionary.db文件的InputStream对象  
  39.                     InputStream is = context.getResources().openRawResource(  
  40.                             R.raw.dictionary);  
  41.                     FileOutputStream fos = new FileOutputStream(databaseFilename);  
  42.                     byte[] buffer = new byte[8192];  
  43.                     int count = 0;  
  44.                     // 开始复制dictionary.db文件  
  45.                     while ((count = is.read(buffer)) > 0)  
  46.                     {  
  47.                         fos.write(buffer, 0, count);  
  48.                     }  
  49.                     //关闭文件流  
  50.                     fos.close();  
  51.                     is.close();  
  52.                 }  
  53.                 // 打开/sdcard/dictionary目录中的dictionary.db文件  
  54.                 SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(  
  55.                         databaseFilename, null);  
  56.                 return database;  
  57.             }  
  58.             catch (Exception e)  
  59.             {  
  60.             }  
  61.             //如果打开出错,则返回null  
  62.             return null;  
  63.         }  
  64.   
  65. }  
  66. </span>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值