Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

Android 應用實例(一):有道辭典查詢 Demo

編輯:開發入門

本節將實作一個簡易的有道辭典查詢範例。使用者可以在輸入框中輸入中文或英文單字,按下查詢按鈕後,程式會透過 WebView 載入有道辭典的行動版搜尋結果。

有道辭典 Demo 畫面一

有道辭典 Demo 畫面二

功能設計

這個範例主要使用四個元件:EditText 用來輸入查詢內容,Button 用來觸發查詢或清空,WebView 用來顯示查詢結果,Toast 則用於提示使用者輸入不得為空。

有道辭典查詢流程

main.xml

<?XML version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText
        android:id="@+id/myEditText1"
        android:layout_width="200px"
        android:layout_height="40px"
        android:textSize="18sp"
        android:layout_x="5px"
        android:layout_y="32px" />

    <Button
        android:id="@+id/myButton01"
        android:layout_width="60px"
        android:layout_height="40px"
        android:text="查詢"
        android:layout_x="205px"
        android:layout_y="35px" />

    <Button
        android:id="@+id/myButton02"
        android:layout_width="50px"
        android:layout_height="40px"
        android:text="清空"
        android:layout_x="270px"
        android:layout_y="35px" />

    <WebView
        android:id="@+id/myWebView1"
        android:layout_width="300px"
        android:layout_height="330px"
        android:layout_x="7px"
        android:layout_y="90px"
        android:background="@drawable/black"
        android:focusable="false" />
</AbsoluteLayout>

YouDao.java

package androidApplication.Instance;

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

public class YouDao extends Activity {
    private Button myButton01;
    private Button myButton02;
    private EditText mEditText1;
    private WebView mWebView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myButton01 = (Button) findViewById(R.id.myButton01);
        myButton02 = (Button) findViewById(R.id.myButton02);
        mEditText1 = (EditText) findViewById(R.id.myEditText1);
        mWebView1 = (WebView) findViewById(R.id.myWebView1);

        myButton01.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                String strURI = mEditText1.getText().toString().trim();

                if (strURI.length() == 0) {
                    Toast.makeText(YouDao.this, "查詢內容不能為空!", Toast.LENGTH_LONG).show();
                } else {
                    String strURL = "https://dict.youdao.com/m/search?keyfrom=dict.mindex&q=" + strURI;
                    mWebView1.loadUrl(strURL);
                }
            }
        });

        myButton02.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mEditText1.setText("");
            }
        });
    }
}

這個範例展示了 Android 表單元件、事件監聽與 WebView 載入網頁的整合方式。若要實際發布,還需要處理網路權限、字元編碼與錯誤提示等細節。

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