Android之离线词典
16lz
2021-12-04
1. 首先在res/raw中导入文件dictionary.db/Files/lee0oo0/dictionary.rar
2. main.xml文件的布局
<? xmlversion="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" >
< AutoCompleteTextView android:id ="@+id/actvWord"
android:layout_width ="fill_parent" android:layout_height ="wrap_content"
android:layout_marginTop ="10dp" android:singleLine ="true" />
< Button android:id ="@+id/btnSelectWord" android:layout_width ="wrap_content"
android:layout_height ="wrap_content" android:text ="查单词" />
3.AutoCompleteTextView的每个textview下拉布局
<? xmlversion="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:textAppearance ="?android:attr/textAppearanceLarge"
android:gravity ="center_vertical"
android:paddingLeft ="6dip"
android:textColor ="#000"
android:minHeight ="?android:attr/listPreferredItemHeight" />
4. 主程序,都几乎带有注释
packagenet.blogjava.mobile;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.os.Bundle;
importandroid.text.Editable;
importandroid.text.TextWatcher;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.View.OnClickListener;
importandroid.widget.AutoCompleteTextView;
importandroid.widget.Button;
importandroid.widget.CursorAdapter;
importandroid.widget.TextView;
public classMain extendsActivity implementsOnClickListener,TextWatcher
{
// dictionary.db的储存目录
private finalStringDATABASE_PATH=android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+"/dictionary";
privateAutoCompleteTextViewactvWord;
private finalStringDATABASE_FILENAME="dictionary.db";
privateSQLiteDatabasedatabase;
privateButtonbtnSelectWord;
@Override
public voidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 打开Database
database=openDatabase();
btnSelectWord=(Button)findViewById(R.id.btnSelectWord);
actvWord=(AutoCompleteTextView)findViewById(R.id.actvWord);
// 设置按钮监听
btnSelectWord.setOnClickListener( this);
// 设施字符改变监听
actvWord.addTextChangedListener( this);
}
public classDictionaryAdapter extendsCursorAdapter
{
privateLayoutInflaterlayoutInflater;
@Override
publicCharSequenceconvertToString(Cursorcursor)
{
returncursor== null?"":cursor.getString(cursor
.getColumnIndex("_id"));
}
private voidsetView(Viewview,Cursorcursor)
{
TextViewtvWordItem=(TextView)view;
tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));
}
@Override
public voidbindView(Viewview,Contextcontext,Cursorcursor)
{
setView(view,cursor);
}
@Override
publicViewnewView(Contextcontext,Cursorcursor,ViewGroupparent)
{
// 把布局文件转换为View对象
Viewview=layoutInflater.inflate(R.layout.word_list_item, null);
setView(view,cursor);
returnview;
}
publicDictionaryAdapter(Contextcontext,Cursorc, booleanautoRequery)
{
super(context,c,autoRequery);
// 通过系统服务获得该上下文的LayoutInflater对象
layoutInflater=(LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}
public voidafterTextChanged(Editables)
{
// 必须将english字段的别名设为_id
Cursorcursor=database.rawQuery(
"selectenglishas_idfromt_wordswhereenglishlike?",
newString[]
{s.toString()+"%"});
DictionaryAdapterdictionaryAdapter= newDictionaryAdapter( this,
cursor, true);
actvWord.setAdapter(dictionaryAdapter);
}
public voidbeforeTextChanged(CharSequences, intstart, intcount,
intafter)
{
// TODOAuto-generatedmethodstub
}
public voidonTextChanged(CharSequences, intstart, intbefore, intcount)
{
// TODOAuto-generatedmethodstub
}
public voidonClick(Viewview)
{
// 从t_words的表中搜索english为输入框中的中文
Stringsql="selectchinesefromt_wordswhereenglish=?";
Cursorcursor=database.rawQuery(sql, newString[]
{actvWord.getText().toString()});
Stringresult="未找到该单词.";
// 如果查找单词,显示其中文的意思
if(cursor.getCount()>0)
{
// 必须使用moveToFirst方法将记录指针移动到第1条记录的位置
cursor.moveToFirst();
result=cursor.getString(cursor.getColumnIndex("chinese"));
}
// 显示查询结果对话框
newAlertDialog.Builder( this).setTitle("查询结果").setMessage(result)
.setPositiveButton("关闭", null).show();
}
privateSQLiteDatabaseopenDatabase()
{
try
{
// 获得dictionary.db文件的绝对路径
StringdatabaseFilename=DATABASE_PATH+"/"+DATABASE_FILENAME;
Filedir= newFile(DATABASE_PATH);
// 如果/sdcard/dictionary目录中存在,创建这个目录
if(!dir.exists())
dir.mkdir();
// 如果在/sdcard/dictionary目录中不存在
// dictionary.db文件,则从res\raw目录中复制这个文件到
// SD卡的目录(/sdcard/dictionary)
if(!( newFile(databaseFilename)).exists())
{
// 获得封装dictionary.db文件的InputStream对象
InputStreamis=getResources().openRawResource(
R.raw.dictionary);
FileOutputStreamfos= newFileOutputStream(databaseFilename);
byte[]buffer= new byte[8192];
intcount=0;
// 开始复制dictionary.db文件
while((count=is.read(buffer))>0)
{
fos.write(buffer,0,count);
}
fos.close();
is.close();
}
// 打开/sdcard/dictionary目录中的dictionary.db文件
SQLiteDatabasedatabase=SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
returndatabase;
}
catch(Exceptione)
{
}
return null;
}
}
更多相关文章
- 类和 Json对象
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android全屏设置的两种方式
- android 模拟器使用指导
- android操作xml
- socket连接
- Android(安卓)TextView背景色、圆角、内部填充设置
- 31、android打开文件夹并列出里面的文件