Android实现有道辞典查询功能实例详解

所属分类: 软件编程 / Android 阅读数: 40
收藏 0 赞 0 分享

本文实例讲述了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("");
   }
  });
 }
}

程序大功告成。其实大家会发现,这个应用相当简单,只是你们没有想到而已,Narcissism一下呵呵~。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android资源操作技巧汇总》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多