MainActivity.java
public class MainActivity extends Activity {
private wordDao wordDao;
private EditText etdanci;
private TextView tvxianshi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chushihua();
}
private void chushihua() {
etdanci = (EditText) findViewById(R.id.etdanci);
tvxianshi = (TextView) findViewById(R.id.tvxianshi);
// TODO Auto-generated method stub
}
@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 chaxundanci(View view){
String english = etdanci.getText().toString();
wordDao = new wordDao(this);
if (TextUtils.isEmpty(english)) {
tvxianshi.setText("请输入您想查询的单词");
} else {
String chinese = wordDao.getchinese(english);
tvxianshi.setText(chinese);
}
}
}
wordDao.java
public class wordDao {
private DBfuzhu dbfuzhu;
private SQLiteDatabase sqLiteDatabase;
public wordDao(Context context){
dbfuzhu = new DBfuzhu(context);
}
public String getchinese(String english){
sqLiteDatabase = dbfuzhu.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;
}
}
DBfuzhu.java
public class DBfuzhu {
private final int Huanchongqu = 400000;
public static final String Shujukuname = "dictionary.db";
public static final String Baoming = "com.example.cidian";
public static final String shujukulujing = "/data" + Environment.getDataDirectory().getAbsolutePath() + Baoming + "/databases";
private Context context;
public DBfuzhu(Context context) {
this.context = context;
}
public SQLiteDatabase openDatabase() {
try {
File myDataPath = new File(shujukulujing);
if (!myDataPath.exists()) {
myDataPath.mkdirs();
}
String dbfile = myDataPath + "/" + Shujukuname;
if (!(new File(dbfile).exists())) {
InputStream is = context.getResources().openRawResource(R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[Huanchongqu];
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;
}
}
dancishiti.java
public class dancishiti {
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;
}
}
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/mainbg"
tools:context=".MainActivity" >
<EditText
android:id="@+id/etdanci"
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/shuru"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvxianshi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/etdanci"
android:layout_below="@+id/etdanci"
android:layout_marginTop="22dp"
android:textSize="25sp"
android:background="#55FFFFFF"
android:radius="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnchaxun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tvxianshi"
android:layout_alignRight="@+id/tvxianshi"
android:background="@drawable/ibsearchword"
android:onClick="chaxundanci"
android:text="@string/chaxun" />
</RelativeLayout>
public class MainActivity extends Activity {
private wordDao wordDao;
private EditText etdanci;
private TextView tvxianshi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chushihua();
}
private void chushihua() {
etdanci = (EditText) findViewById(R.id.etdanci);
tvxianshi = (TextView) findViewById(R.id.tvxianshi);
// TODO Auto-generated method stub
}
@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 chaxundanci(View view){
String english = etdanci.getText().toString();
wordDao = new wordDao(this);
if (TextUtils.isEmpty(english)) {
tvxianshi.setText("请输入您想查询的单词");
} else {
String chinese = wordDao.getchinese(english);
tvxianshi.setText(chinese);
}
}
}
wordDao.java
public class wordDao {
private DBfuzhu dbfuzhu;
private SQLiteDatabase sqLiteDatabase;
public wordDao(Context context){
dbfuzhu = new DBfuzhu(context);
}
public String getchinese(String english){
sqLiteDatabase = dbfuzhu.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;
}
}
DBfuzhu.java
public class DBfuzhu {
private final int Huanchongqu = 400000;
public static final String Shujukuname = "dictionary.db";
public static final String Baoming = "com.example.cidian";
public static final String shujukulujing = "/data" + Environment.getDataDirectory().getAbsolutePath() + Baoming + "/databases";
private Context context;
public DBfuzhu(Context context) {
this.context = context;
}
public SQLiteDatabase openDatabase() {
try {
File myDataPath = new File(shujukulujing);
if (!myDataPath.exists()) {
myDataPath.mkdirs();
}
String dbfile = myDataPath + "/" + Shujukuname;
if (!(new File(dbfile).exists())) {
InputStream is = context.getResources().openRawResource(R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[Huanchongqu];
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;
}
}
dancishiti.java
public class dancishiti {
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;
}
}
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/mainbg"
tools:context=".MainActivity" >
<EditText
android:id="@+id/etdanci"
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/shuru"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvxianshi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/etdanci"
android:layout_below="@+id/etdanci"
android:layout_marginTop="22dp"
android:textSize="25sp"
android:background="#55FFFFFF"
android:radius="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnchaxun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tvxianshi"
android:layout_alignRight="@+id/tvxianshi"
android:background="@drawable/ibsearchword"
android:onClick="chaxundanci"
android:text="@string/chaxun" />
</RelativeLayout>