1.实现有道词典需要拖拽一个EditText,一个Button,以及一个TextView。其具体代码如下:
activity类的代码:
111 12 25 26 27 28 29 40 41 51 52
2.MainActivity类的代码:
1 package com.example.happydictionary; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.text.TextUtils; 6 import android.view.Menu; 7 import android.view.View; 8 import android.widget.EditText; 9 import android.widget.TextView;10 11 import com.example.happydictionary.dao.WordDao;12 13 public class MainActivity extends Activity {14 private WordDao wordDao;15 private EditText etWord;16 private TextView tvResult;17 18 @Override19 protected void onCreate(Bundle savedInstanceState) {20 super.onCreate(savedInstanceState);21 setContentView(R.layout.activity_main);22 23 initViews();24 }25 26 private void initViews() {27 etWord = (EditText) findViewById(R.id.etWord);28 tvResult = (TextView) findViewById(R.id.tvSearchResult);29 }30 31 @Override32 public boolean onCreateOptionsMenu(Menu menu) {33 // Inflate the menu; this adds items to the action bar if it is present.34 getMenuInflater().inflate(R.menu.main, menu);35 return true;36 }37 38 public void searchWord(View view) {39 String english = etWord.getText().toString();40 wordDao = new WordDao(this);41 if (TextUtils.isEmpty(english)) {42 tvResult.setText("请输入您想查询的单词");43 } else {44 String chinese = wordDao.getChinese(english);45 tvResult.setText(chinese);46 }47 }48 49 }
3.WordDao类的代码:
1 package com.example.happydictionary.dao; 2 3 import android.content.Context; 4 import android.database.Cursor; 5 import android.database.sqlite.SQLiteDatabase; 6 7 import com.example.happydictionary.db.DBHelper; 8 9 public class WordDao {10 private DBHelper dbHelper;11 private SQLiteDatabase sqLiteDatabase;12 public WordDao(Context context){13 dbHelper=new DBHelper(context);14 }15 public String getChinese(String english){16 sqLiteDatabase=dbHelper.openDatabase();17 String sql="select chinese from t_words where english=?";18 Cursor cursor=sqLiteDatabase.rawQuery(sql, new String[]{english});19 String chinese="查无该词";20 if(cursor.moveToFirst()){21 chinese=cursor.getString(cursor.getColumnIndex("chinese"));22 }23 return chinese;24 }25 }
4.DBHelper类的代码:
1 package com.example.happydictionary.db; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 9 import com.example.happydictionary.R;10 import com.example.happydictionary.R.raw;11 12 import android.content.Context;13 import android.database.sqlite.SQLiteDatabase;14 import android.os.Environment;15 import android.util.Log;16 17 /**18 * 实现将数据库文件从raw目录拷贝到手机里存放数据库的位置19 * 20 * @author cabbage21 */22 public class DBHelper {23 private final int BUFFER_SIZE = 400000;24 public static final String DB_NAME = "idiom.db"; // 保存的数据库文件名25 public static final String PACKAGE_NAME = "com.example.happydictionary";// 应用的包名26 public static final String DB_PATH = "/data"27 + Environment.getDataDirectory().getAbsolutePath() + "/"28 + PACKAGE_NAME + "/databases";29 /*// SDCard 定义数据库的存放路径30 private final String DATABASE_PATH = android.os.Environment31 .getExternalStorageDirectory().getAbsolutePath() + "/dictionary";*/32 33 private Context context;34 35 public DBHelper(Context context) {36 this.context = context;37 }38 39 public SQLiteDatabase openDatabase() {40 try {41 File myDataPath = new File(DB_PATH);42 if (!myDataPath.exists()) {43 myDataPath.mkdirs();// 如果没有这个目录则创建44 }45 String dbfile = myDataPath + "/" + DB_NAME;46 if (!(new File(dbfile).exists())) { // 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库47 InputStream is = context.getResources().openRawResource(48 R.raw.dictionary); // 欲导入的数据库49 FileOutputStream fos = new FileOutputStream(dbfile);50 byte[] buffer = new byte[BUFFER_SIZE];51 int count = 0;52 while ((count = is.read(buffer)) > 0) {53 fos.write(buffer, 0, count);54 }55 fos.close();56 is.close();57 }58 SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,59 null);60 return db;61 } catch (FileNotFoundException e) {62 Log.e("Database", "File not found");63 e.printStackTrace();64 } catch (IOException e) {65 Log.e("Database", "IO exception");66 e.printStackTrace();67 }68 return null;69 }70 }
5.Word类的代码:
1 package com.example.happydictionary.entity; 2 3 public class Word { 4 private String english; 5 private String chinese; 6 7 public String getEnglish() { 8 return english; 9 }10 11 public void setEnglish(String english) {12 this.english = english;13 }14 15 public String getChinese() {16 return chinese;17 }18 19 public void setChinese(String chinese) {20 this.chinese = chinese;21 }22 23 }
6.DBHelperTest类的代码:
1 package com.example.happydictionary.test; 2 3 import com.example.happydictionary.db.DBHelper; 4 5 import android.test.AndroidTestCase; 6 7 public class DBHelperTest extends AndroidTestCase { 8 public void testCreateDB(){ 9 DBHelper dbHelper=new DBHelper(getContext());10 dbHelper.openDatabase();11 }12 }