Android應用執行個體(一)之—有道辭典VZ.0

來源:互聯網
上載者:User

大家好,這是我做的一個簡單的有道Android的DEMO,只是簡單的雛形。介面設計也有點醜陋呵呵~ 看看下面的:

 

 

 

第一步:思路解析

 

從介面看一共用了三個控制項EditText,Button,WebView。其實是四個,是當我們查詢內容為空白的時候用來提示的Toast控制項。

我們在EditText輸入查詢內容,這裡包括中文,英文。然後通過參數的形式,從http://dict.youdao.com/m取出資料把結果

存放在WebView裡。

 

如所示:

 

第二步:入手程式。

 

首先是布局介面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("");
      }
    });
  }
}

 

程式大功告成。其實大家會發現,這個應用相當簡單,只是你們沒有想到而已,Narcissism一下呵呵~。如果大家程式跑不起來

,可以Q我Email我,在留言裡留下你們的Q或者Email.

 

 

今天就到此為止! 謝謝大家~

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.