调用有道词典
DbHelper类进行数据操作,activity_mian.xml布局页面,MainAvtivity类实现继承与查询等操作。
MainAvtivity.java:
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 }
activity_mian.xml:
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:ems="10" 20 android:hint="@string/searchHint" 21 android:singleLine="true" 22 android:textColor="#552006" 23 android:textColorHint="#782f10" > 24 25 <requestFocus /> 26 </EditText> 27 28 <Button 29 android:id="@+id/btnSearch" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_alignBaseline="@+id/etWord" 33 android:layout_alignBottom="@+id/etWord" 34 android:layout_marginLeft="16dp" 35 android:layout_toRightOf="@+id/etWord" 36 android:background="@drawable/ibsearchword" 37 android:onClick="searchWord" 38 android:text="@string/serachWord" /> 39 40 <TextView 41 android:id="@+id/tvSearchResult" 42 android:layout_width="match_parent" 43 android:layout_height="match_parent" 44 android:layout_alignLeft="@+id/etWord" 45 android:layout_below="@+id/etWord" 46 android:layout_marginTop="22dp" 47 android:textSize="25sp" 48 android:textAppearance="?android:attr/textAppearanceMedium" /> 49 50 </RelativeLayout>
DbHelper类:
1 import java.io.File; 2 import java.io.FileNotFoundException; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 7 import com.example.happydictionary.R; 8 import com.example.happydictionary.R.raw; 9 10 import android.content.Context; 11 import android.database.sqlite.SQLiteDatabase; 12 import android.os.Environment; 13 import android.util.Log; 14 public class DBHelper { 15 private final int BUFFER_SIZE = 400000; 16 public static final String DB_NAME = “idiom.db”; 17 public static final String PACKAGE_NAME = “com.example.happydictionary”; 18 public static final String DB_PATH = “/data” 19 + Environment.getDataDirectory().getAbsolutePath() + “/” 20 + PACKAGE_NAME + “/databases”; 21 private Context context; 22 23 public DBHelper(Context context) { 24 this.context = context; 25 } 26 27 public SQLiteDatabase openDatabase() { 28 try { 29 File myDataPath = new File(DB_PATH); 30 if (!myDataPath.exists()) { 31 myDataPath.mkdirs(); 32 } 33 String dbfile = myDataPath + "/" + DB_NAME; 34 if (!(new File(dbfile).exists())) { 35 InputStream is = context.getResources().openRawResource( 36 R.raw.dictionary); 37 FileOutputStream fos = new FileOutputStream(dbfile); 38 byte[] buffer = new byte[BUFFER_SIZE]; 39 int count = 0; 40 while ((count = is.read(buffer)) > 0) { 41 fos.write(buffer, 0, count); 42 } 43 fos.close(); 44 is.close(); 45 } 46 SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, 47 null); 48 return db; 49 } catch (FileNotFoundException e) { 50 Log.e("Database", "File not found"); 51 e.printStackTrace(); 52 } catch (IOException e) { 53 Log.e("Database", "IO exception"); 54 e.printStackTrace(); 55 } 56 return null; 57 }
时间: 2024-10-13 11:39:27