Android之离线词典
1. 首先在res/raw中导入文件dictionary.db/Files/lee0oo0/dictionary.rar
2. main.xml文件的布局
<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下拉布局
<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. 主程序,都几乎带有注释
package net.blogjava.mobile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
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.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 Main extends Activity implements OnClickListener, TextWatcher
{
//dictionary.db的储存目录
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;
@Override
public void onCreate(Bundle savedInstanceState)
{
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 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 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 = (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() + "%" });
DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,
cursor, true);
actvWord.setAdapter(dictionaryAdapter);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
}
public void onClick(View view)
{
//从t_words的表中搜索english为输入框中的中文
String sql = "select chinese from t_words where english=?";
Cursor cursor = database.rawQuery(sql, new String[]
{ actvWord.getText().toString() });
String result = "未找到该单词.";
// 如果查找单词,显示其中文的意思
if (cursor.getCount() > 0)
{
// 必须使用moveToFirst方法将记录指针移动到第1条记录的位置
cursor.moveToFirst();
result = cursor.getString(cursor.getColumnIndex("chinese"));
}
// 显示查询结果对话框
new AlertDialog.Builder(this).setTitle("查询结果").setMessage(result)
.setPositiveButton("关闭", null).show();
}
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;
}
}
转载于:https://www.cnblogs.com/lee0oo0/archive/2012/03/29/2422992.html
Android之离线词典相关推荐
- 基于android的电子词典设计_基于Android的电子词典软件的设计与实现 (含外文翻译).doc...
基于Android的电子词典软件的设计与实现 (含外文翻译) LANZHOU UNIVERSITY OF TECHNOLOGY 毕业设计 题 目 基于Android的电子词典软件的设计与实现 学生姓名 ...
- android词典论文,基于Android的电子词典软件的设计与实现毕业论文doc.doc
基于Android的电子词典软件的设计与实现毕业论文doc 调-- 乂拿LANZHOU UNIVERSITY OF TECHNOLOGY毕兆设计题目基于Android的电子词典软件的ig计与剪见学生姓 ...
- android词典论文,基于Android的电子词典软件的设计与实现(毕业论文doc)资料.doc...
基于Android的电子词典软件的设计与实现(毕业论文doc)资料 调-- 乂拿LANZHOU UNIVERSITY OF TECHNOLOGY毕兆设计题目基于Android的电子词典软件的ig计与剪 ...
- 有道云笔记Android app离线缓存,有道云笔记Android版升级 增加笔记离线阅读与收藏...
近日,有道云笔记Android版升级至2.5版,增加了iPhone版中颇受用户好评的离线阅读功能,同时增加了微信分享笔记,收藏笔记等实用功能. 升级后,用户可以在离线阅读设置页面,选择需要离线阅读的笔 ...
- android 编程词典,基于Android的英文词典的实现方法
英文词典是手机中经常使用的应用.因此,在本文将结合Android来讨论如何实现一个Android版的英文词典.实现英文词典的方法很多.在本文使用了SQLite数据库来保存英文单词信息.系统通过SQLi ...
- android电子英语词典背景介绍,实现基于Android的英文电子词典
本文为原创,如需转载,请注明作者和出处,谢谢! 英文词典是手机中经常使用的应用.因此,在本文将结合Android来讨论如何实现一个Android版的英文词典.实现英文词典的方法很多.在本文使用了SQL ...
- Android应用离线中文语音识别PocketSphinx (免费哦~)
该文章重点在中文语音识别的应用上 目录 前言 一.不太推荐的知名demo 1.官方demo PocketSphinx (https://github.com/cmusphinx/pocketsphin ...
- Android SDK 离线下载安装更新至4.0.3(目前最新) (转)
原文地址为: Android SDK 离线下载安装更新至4.0.3(目前最新) (转) 转自:http://hi.baidu.com/xiaoyun1227/item/e0ca2ccc06cf6d35 ...
- 基于android的电子词典设计_基于Android的电子词典的设计与实现.doc
摘要:随着信息技术的飞速发展,智能手机的普及应用,手机软件的使用也得到了很好的发展.而当下android操作系统的手机是最为常见的手机操作系统之一,加之人们对学习英语的热度不断提高.因此设计一款便捷高 ...
- android sdk linux 离线包,android sdk离线安装
android sdk离线安装 如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!先分别下载所需包,再安装. 一,首先下载SDK的安装包,android-sdk_r06-windo ...
最新文章
- c语言程序设计样板,《C语言程序设计》课程设计报告书样板.doc
- 关于xib里面的NSLayoutConstraint的multiplier修改
- PS怎么将自己的形状存储为自定形状?
- Asp.Net Membership 回顾
- Keras学习笔记:函数式模型
- 推荐系统炼丹笔记:边缘计算+奉送20个推荐系统强特
- 60 个神级 VS Code 插件!
- SpiderData 2019年2月27日 DApp数据排行榜
- java提高篇之理解java的三大特性——多态
- k2677场效应管参数引脚_共射极放大电路,场效应管放大电路,运算放大电路
- lxml读取本地html文件,如何使用Python和lxml来解析本地html文件?
- 计算机系统操作技师考试题,机关事业单位技术工人计算机操作技师考试题库
- testflight测试软件怎么,TestFlight使用步骤
- 漫画:什么是字符串匹配算法?
- Glib实例学习(1)单链表
- 第三方魔兽金币交易平台的影响力会很快地褪去
- AnyLogic建模笔记
- csp是什么比赛_有关CSPJ/S比赛问题
- jink下载出现:Failed to download RAMCode . Failed to prepare for programming .
- wangEditor富文本自定义图片宽度