1.实现有道词典需要拖拽一个EditText,一个Button,以及一个TextView。其具体代码如下:
activity类的代码:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@drawable/mainbg" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity" > 11 12 <EditText 13 android:id="@+id/etWord" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_alignParentLeft="true" 17 android:layout_alignParentTop="true" 18 android:layout_marginTop="31dp" 19 android:background="@android:drawable/edit_text" 20 android:ems="10" 21 android:hint="@string/searchHint" 22 android:singleLine="true" 23 android:textColor="#552006" 24 android:textColorHint="#782f10" > 25 26 <requestFocus /> 27 </EditText> 28 29 <Button 30 android:id="@+id/btnSearch" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:layout_alignBaseline="@+id/etWord" 34 android:layout_alignBottom="@+id/etWord" 35 android:layout_marginLeft="16dp" 36 android:layout_toRightOf="@+id/etWord" 37 android:background="@drawable/ibsearchword" 38 android:onClick="searchWord" 39 android:text="@string/serachWord" /> 40 41 <TextView 42 android:id="@+id/tvSearchResult" 43 android:layout_width="match_parent" 44 android:layout_height="match_parent" 45 android:layout_alignLeft="@+id/etWord" 46 android:layout_below="@+id/etWord" 47 android:layout_marginTop="22dp" 48 android:textSize="25sp" 49 android:background="@drawable/bg_roundcorner" 50 android:textAppearance="?android:attr/textAppearanceMedium" /> 51 52 </RelativeLayout>
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 @Override 19 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 @Override 32 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 cabbage 21 */ 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.Environment 31 .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 }
时间: 2024-10-10 04:35:22