Android實現有道辭典查詢功能實例詳解
本文實例講述了Android實現有道辭典查詢功能的方法。分享給大家供大家參考,具體如下:
這是我做的一個簡單的有道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(""); } }); } }
程序大功告成。其實大家會發(fā)現,這個應用相當簡單,只是你們沒有想到而已,Narcissism一下呵呵~。
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
viewPager+fragment刷新緩存fragment的方法
這篇文章主要介紹了viewPager+fragment刷新緩存fragment的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03詳解Android 在 ViewPager 中使用 Fragment 的懶加載
本篇文章主要介紹了Android 在 ViewPager 中使用 Fragment 的懶加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Android文本輸入框(EditText)輸入密碼時顯示與隱藏
這篇文章主要介紹了Android文本輸入框(EditText)輸入密碼時顯示與隱藏的方法和示例,需要的朋友可以參考下2014-12-12