有道词典

来源:互联网 发布:网络选修课app 编辑:程序博客网 时间:2024/06/10 08:28






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" >


    <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:background="@android:drawable/edit_text"
        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:background="@drawable/bg_roundcorner"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout><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:background="@android:drawable/edit_text"
        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:background="@drawable/bg_roundcorner"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout><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:background="@android:drawable/edit_text"
        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:background="@drawable/bg_roundcorner"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout><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:background="@android:drawable/edit_text"
        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:background="@drawable/bg_roundcorner"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout><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:background="@android:drawable/edit_text"
        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:background="@drawable/bg_roundcorner"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout><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:background="@android:drawable/edit_text"
        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:background="@drawable/bg_roundcorner"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">电子词典</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="searchHint">请输入您要查询的单词</string>
    <string name="serachWord">查询</string>


</resources>

WordDao.java

package com.example.happydictionary.dao;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.example.happydictionary.db.DBHelper;


public class WordDao {
private DBHelper dbHelper;
private SQLiteDatabase sqLiteDatabase;
public WordDao(Context context){
dbHelper=new DBHelper(context);
}
     public String getChinese(String english){
    sqLiteDatabase=dbHelper.openDatabase();
    String sql="select chinese from t_words where english=?";
    Cursor cursor=sqLiteDatabase.rawQuery(sql, new String[]{english});
    String chinese="查无该词";
    if(cursor.moveToFirst()){
    chinese=cursor.getString(cursor.getColumnIndex("chinese"));
    }
    return chinese;
     }
}

DBHelper,java

package com.example.happydictionary.db;


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;


/**
 * 实现将数据库文件从raw目录拷贝到手机里存放数据库的位置
 * 
 * @author cabbage
 */
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";
/*// SDCard 定义数据库的存放路径
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath() + "/dictionary";*/


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;
}
}



Word.java

package com.example.happydictionary.entity;


public class Word {
private String english;
private String chinese;


public String getEnglish() {
return english;
}


public void setEnglish(String english) {
this.english = english;
}


public String getChinese() {
return chinese;
}


public void setChinese(String chinese) {
this.chinese = chinese;
}


}


0 0
原创粉丝点击