Android 有道词典的简单实现方法介绍
首先看程序界面如下!
1、布局文件:
<absolutelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<edittext
android:id="@+id/edittext"
android:layout_width="150px"
android:layout_height="40px"
android:layout_x="5px"
android:layout_y="32px"
android:textsize="18sp" />
<button
android:id="@+id/btnsearch"
android:layout_width="60px"
android:layout_height="40px"
android:layout_x="165px"
android:layout_y="35px"
android:text="查询" />
<button
android:id="@+id/btnclear"
android:layout_width="60px"
android:layout_height="40px"
android:layout_x="230px"
android:layout_y="35px"
android:text="清空" />
<webview
android:id="@+id/reswebview"
android:layout_width="300px"
android:layout_height="330px"
android:layout_x="7px"
android:layout_y="90px"
android:focusable="false" />
</absolutelayout>
2、修改mainactivity:
public class mainactivity extends activity {
private button btnsearch;
private button btnclear;
private edittext edittext;
private webview reswebview;
private void setview() {
btnsearch = (button) findviewbyid(r.id.btnsearch);
btnclear = (button) findviewbyid(r.id.btnclear);
edittext = (edittext) findviewbyid(r.id.edittext);
reswebview = (webview) findviewbyid(r.id.reswebview);
btnsearch.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
string struri = edittext.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);
}
}
});
btnclear.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
edittext.settext("");
}
});
}
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
setview();
}
}
3、在清单文件中添加网络访问权限:
<uses-permission android:name="android.permission.internet" />
运行程序即可!