大家好,這是我做的一個簡單的有道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.
今天就到此為止! 謝謝大家~