Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 有道詞典的簡易實作教學

編輯:關於 Android 程式設計

本文將動手打造一個迷你版的「有道詞典」:使用者在輸入框輸入單字,點擊查詢後,透過 WebView 載入有道詞典的線上查詢結果頁面即可顯示翻譯。這是一個結合 EditText、Button 與 WebView 的實用小範例。

步驟一:佈局檔

使用 AbsoluteLayout 以絕對座標排列各控制項:

<AbsoluteLayout xmlns:android="https://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>

步驟二:修改 MainActivity

點擊查詢時,把輸入的單字拼接到有道詞典的查詢網址上,再交由 WebView 載入;點擊清空則重設輸入框:

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().trim();
                if (strUri.length() == 0) {
                    Toast.makeText(getApplicationContext(), "請輸入查詢字符", 1).show();
                } else {
                    String strURL = "https://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();
    }
}

步驟三:加上網路權限

由於需要載入線上網頁,別忘了在清單檔中宣告網路權限:

<uses-permission android:name="android.permission.INTERNET" />

運行程式,輸入單字並點擊查詢,有道詞典的翻譯結果便會顯示在 WebView 中。這個範例示範了如何以最簡潔的方式,把 WebView 化為一個功能完整的線上查詢視窗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved