首先看程式介面如下!
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" />
運行程式即可!