English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Androidで有道辞書の検索機能を実現する例と詳細

この記事では、AndroidでYouDao辞書の検索機能を実現する方法について説明しています。皆さんに共有し、以下のように詳細に説明します:

これは私が作成したシンプルなYouDao AndroidのDEMOです。単なる骨組みです。インターフェースのデザインも少し見苦しいです呵呵~ 下の図を確認してください:

第1段:思考解析

インターフェースから見ると、EditText、Button、WebViewの3つのコントロールを使用しています。実際には4つです。クエリ内容が空の場合に使用されるToastコントロールです。

私たちがEditTextにクエリ内容を入力し、ここには中国語、英語が含まれています。それから、パラメータとして、以下からhttp://dict.youdao.com/mデータを取り出して結果
WebViewに保存されています。

以下の図のように表示されています:

第2段:プログラムの入手

まずはレイアウトインターフェースmain.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
 xmlns:android="http://schemas.android.com/apk/res/android
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <!-- 一個EditTextを作成します -->
 <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を作成します -->
 <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_height="40px"
  android:layout_width="50px"
  android:text="クリア"
  android:layout_y="35px"
  android:layout_x="270px"
 />
 <!-- 一個WebViewを作成します -->
 <WebView
 android:id="@"+id/myWebView1"
 android:layout_height="330px"
 android:layout_width="300px"
 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;
 //加载数据的WebView声明
 private WebView mWebView1;
 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());
     strURI = strURI.trim();
     //如果查询内容为空提示
     if (strURI.length() == 0)
     {
      Toast.makeText(YouDao.this, "查询内容不能为空!", Toast.LENGTH_LONG)
        .show();
     }
     //否则则以参数的形式从http:\/\///dict.youdao.com/m取得数据,加载到WebView里.
     else
     {
      String strURL = "http:\/\/"//dict.youdao.com/m/search?keyfrom=dict.mindex&q="
        + strURI;
      mWebView1.loadUrl(strURL);
     }
    }
  });
  //クリアボタンにイベントを追加し、EditTextを空にします
  myButton02.setOnClickListener(new Button.OnClickListener()
  {
   public void onClick(View v)
   {
    mEditText1.setText("");
   }
  });
 }
}

プログラムが完成しました。実際には、このアプリケーションは非常にシンプルであり、ただあなたが思いもしなかっただけです、ナルシシズムの一つで、呵呵~。

Androidに関するより多くの内容に興味を持つ読者は、以下の本サイトの特集を確認してください:《Android開発入門と進階ガイド》、《AndroidビューView技術の要約》、《Androidプログラミングでのactivity操作技術の要約》、《AndroidSQLiteデータベース操作技術の要約》、《Androidjson形式データ操作技術の要約》、《Androidデータベース操作技術の要約》、《Androidファイル操作技術の総合》、《Androidプログラミング開発でのSDカード操作方法の総合》、《Androidリソース操作技術の総合》および《Androidコントロールの使用方法の要約》

このガイドが皆様のAndroidプログラムデザインに役立つことを願っています。

声明:本文の内容はインターネットから取得しており、著作権者に帰属します。インターネットユーザーが自発的に貢献し、自己でアップロードしたものであり、本サイトは所有権を有しておらず、人工的な編集処理も行われていません。著作権侵害が疑われる内容がある場合は、以下のメールアドレスまでご連絡ください:notice#oldtoolbag.com(メールを送信する際、#を@に置き換えてください。報告を行い、関連する証拠を提供してください。一旦確認が取れましたら、本サイトは即座に侵害される可能性のあるコンテンツを削除します。)

おすすめ