原理:DbHelper类进行数据操作,activity_mian.xml布局页面,MainAvtivity类实现继承与查询等操作。
MainAvtivity.java:
package com.example.happydictionary;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.happydictionary.dao.WordDao;
public class MainActivity extends Activity {
private WordDao wordDao;
private EditText etWord;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
etWord = (EditText) findViewById(R.id.etWord);
tvResult = (TextView) findViewById(R.id.tvSearchResult);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void searchWord(View view) {
String english = etWord.getText().toString();
wordDao = new WordDao(this);
if (TextUtils.isEmpty(english)) {
tvResult.setText("请输入您想查询的单词");
} else {
String chinese = wordDao.getChinese(english);
tvResult.setText(chinese);
}
}
}
activity_mian.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" >
<EditText
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:ems="10"
android:hint="@string/searchHint"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</EditText>
<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:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
DbHelper类:
“`
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.example.happydictionary.R;
import com.example.happydictionary.R.raw;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;
public class DBHelper {
private final int BUFFER_SIZE = 400000;
public static final String DB_NAME = “idiom.db”;
public static final String PACKAGE_NAME = “com.example.happydictionary”;
public static final String DB_PATH = “/data”
+ Environment.getDataDirectory().getAbsolutePath() + “/”
+ PACKAGE_NAME + “/databases”;
private Context context;
public DBHelper(Context context) {
this.context = context;
}
public SQLiteDatabase openDatabase() {
try {
File myDataPath = new File(DB_PATH);
if (!myDataPath.exists()) {
myDataPath.mkdirs();
}
String dbfile = myDataPath + "/" + DB_NAME;
if (!(new File(dbfile).exists())) {
InputStream is = context.getResources().openRawResource(
R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
null);
return db;
} catch (FileNotFoundException e) {
Log.e("Database", "File not found");
e.printStackTrace();
} catch (IOException e) {
Log.e("Database", "IO exception");
e.printStackTrace();
}
return null;
}