可用android studio直接打开,仿有道词典、谷歌翻译软件制作,在线翻译,提供源码下载!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化普通数据;
initType();
//得到权限
primission();
//初始化查词控件
initWordView();
//初始化搜索框
initView();
//点击输入框外实现软键盘隐藏
searchWords_fatherLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
//创建小悬浮窗口
show(view);
searchWords_voiceE = (ImageButton) findViewById(R.id.searchWords_voiceE);
searchWords_voiceE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wordsAction.playMP3(words.getKey(), "E", MainActivity.this);
}
});
searchWords_voiceA = (ImageButton) findViewById(R.id.searchWords_voiceA);
searchWords_voiceA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wordsAction.playMP3(words.getKey(), "A", MainActivity.this);
}
});
// 清空搜索历史
tv_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//删除记录
deleteData();
//加载数据
queryData("");
}
});
// 修改回车键功能
et_search.setOnKeyListener(new View.OnKeyListener() {// 输入完后按键盘上的搜索键
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
// 先隐藏键盘防止挤原有的布局
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
// 按完搜索键后将当前查询的关键字保存起来,如果该关键字已经存在就不执行保存
boolean hasData = hasData(et_search.getText().toString().trim());
if (!hasData) {
insertData(et_search.getText().toString().trim());
queryData("");
}
}
return false;
}
});
// 搜索框的文本变化实时监听
et_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!et_search.getText().toString().trim().equals(null))
loadWords(et_search.getText().toString().trim());
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().trim().length() == 0) {
tv_tip.setText("搜索历史");
} else {
tv_tip.setText("搜索结果");
}
String tempName = et_search.getText().toString();
// 根据tempName去模糊查询数据库中有没有数据
queryData(tempName);
}
});
//搜索点击的搜查记录item
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (ischech) {
TextView textView = (TextView) view.findViewById(android.R.id.text1);
String name = textView.getText().toString();
et_search.setText(name);
loadWords(name);
}else {
ischech=true;
}
}});
//删除长按的搜查记录item
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
TextView textView = (TextView) view.findViewById(android.R.id.text1);
String name = textView.getText().toString();
db = helper.getWritableDatabase();
db.execSQL("delete from records where name =?", new String[]{name});
db.close();
queryData("");
ischech=false;
return ischech;
}
});}
//创建数据表或者打开数据表
private void init(){
helper=new RecordSQLiteOpenHelper(MainActivity.this);
//加载数据
queryData("");
}
//添加数据
private void insertData(String tempName) {
db = helper.getWritableDatabase();
db.execSQL("insert into records(name) values('" + tempName + "')");
db.close();
}
//加载数据
private void queryData(String tempName) {
Cursor cursor = helper.getReadableDatabase().rawQuery(
//查询records表中的id字段起个别名为“_id”(这是为了下面的创建适配器),条件是name段包含“”
"select id as _id,name from records where name like '%" + tempName + "%' order by id desc ", null);
// 创建adapter适配器对象,这里使用系统定义好的布局文件只显示一行文字
adapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,new String[]{"name"},new int[]{ android.R.id.text1 },CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
// 设置适配器
listView.setAdapter(adapter);
//记住你划到的位置,重新加载数据的时候不会改变位置,只是改变了数据
adapter.notifyDataSetChanged();
}
//检查数据库中是否已经有该条记录
private boolean hasData(String tempName) {
Cursor cursor = helper.getReadableDatabase().rawQuery(
"select id as _id,name from records where name =?", new String[]{tempName});
//判断是否有下一个
loadWords(tempName);
return cursor.moveToNext();
}
private void initView() {
et_search = (EditText) findViewById(R.id.et_search);
tv_tip = (TextView) findViewById(R.id.tv_tip);
listView = (MyListView) findViewById(R.id.listView);
tv_clear = (TextView) findViewById(R.id.tv_clear);
//创建数据表或者打开数据表
init();
// 调整EditText左边的搜索按钮的大小
Drawable drawable = getResources().getDrawable(R.drawable.search);
drawable.setBounds(0, 0, 60, 60);// 第一0是距左边距离,第二0是距上边距离,60分别是长宽
et_search.setCompoundDrawables(drawable, null, null, null);// 只放左边
}
//网络查词完成后回调handleMessage方法
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 111:
//判断网络查找不到该词的情况
if (words.getSent().length() > 0) {
upDateView();
} else {
searchWords_linerLayout.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "抱歉!找不到该词!", Toast.LENGTH_SHORT).show();
}
Log.d("测试", "保存成功");
}
}
};
// 读取words的方法,优先从数据中搜索,没有在通过网络搜索
public void loadWords(String key) {
words = wordsAction.getWordsFromSQLite(key);
if ("" == words.getKey()) {
String address = wordsAction.getAddressForWords(key);
HttpUtil.sentHttpRequest(address, new HttpCallBackListener() {
@Override
public void onFinish(InputStream inputStream) {
WordsHandler wordsHandler = new WordsHandler();
ParseXML.parse(wordsHandler, inputStream);
words = wordsHandler.getWords();
setWords(words);
wordsAction.saveWordsMP3(words);
wordsAction.saveWords(words);
handler.sendEmptyMessage(111);
}
@Override
public void onError() {
}
});
} else {
setWords(words);
//加载数据到界面
upDateView();
}
}