连接有道网页仿写有道词典

此代码是由本地数据库版直接改成导入网页有道词典,并有下拉页面进行自动提示,但是关于最后有道接口实在不会写,所以代码只能运行一半,有会的小伙伴们可以一起交流交流呀~如果只是想要本地数据库支持,只需略改动一下即可。(近几天上传完整版的代码)连接有道网页仿写有道词典_第1张图片

1.MainActivity.java

package com.example.happydictionary;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.happydictionary.dao.WordDao;

public class MainActivity extends Activity {
	private WordDao wordDao;
	private EditText etWord;
	private TextView tvResult;

	// protected void onCreate1(Bundle savedInstanceState) {
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.activity_main);
	//
	// initViews();
	// }

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SetView();
	}

	// private void initViews() {
	// // TODO Auto-generated method stub
	//
	// }

	private Button btnSearch;
	// private EditText editText;
	private WebView reswebView;
	private WebView wvSearchResult;

	private void SetView() {
		btnSearch = (Button) findViewById(R.id.btnSearch);
//		etWord = (EditText) findViewById(R.id.etWord);
		wvSearchResult = (WebView) findViewById(R.id.wvSearchResult);
		/**
		 * 原来的editText换成了AutoCompleteTextView
		 */
		String[] countries = new String[] { "Afghanistan", "Albania",
				"Algeria", "American Samoa", "Andorra" };// 这里只举例说明,数据较少
		AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.etWord);// 找到相应的控件
		ArrayAdapter adapter = new ArrayAdapter(this,
				R.layout.list_item, countries);// 配置Adaptor
		autoCompleteTextView.setAdapter(adapter);
		
		
		btnSearch.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String strUri = etWord.getText().toString();
				strUri = strUri.trim();
				if (strUri.length() == 0) {
					Toast.makeText(getApplicationContext(), "请输入查询字符", 1)
							.show();
				} else {
					String strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q="
							+ strUri;
					//这个地方抛出空指针异常,没办法,不知道这个有道接口格式
					reswebView.loadUrl(strURL);
				}
			}
		});

	}

}

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

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

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

}

5.DBhelperTest.java
package com.example.happydictionary.test;

import com.example.happydictionary.db.DBHelper;

import android.test.AndroidTestCase;

public class DBHelperTest extends AndroidTestCase {
     public void testCreateDB(){
    	 DBHelper dbHelper=new DBHelper(getContext());
    	 dbHelper.openDatabase();
     }
}
6.Activity_mian.xml



  
        
     

        

    

7.list_item.xml



8.strings.xml




    电子词典
    Settings
    Hello world!
    请输入您要查询的单词
    查询





你可能感兴趣的:(连接有道网页仿写有道词典)