有道词典Demo(WebView)

原创 2015年07月08日 20:40:58

学习了WebView组件,写了一个有道词典的小案例

 

 

 

 

效果图如下:

:

 

 

 

 

 

 

源码下载地址:https://coding.net/u/gxs1225/p/YouDaoDictionary/git

代码如下:
布局

activity_main.xml

<code class="hljs xml has-numbering"><span class="hljs-tag"><<span class="hljs-title">RelativeLayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>
    <span class="hljs-attribute">xmlns:tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span>
    <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span>
    <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span>
    <span class="hljs-attribute">android:background</span>=<span class="hljs-value">"@drawable/mainbg"</span>
    <span class="hljs-attribute">android:paddingBottom</span>=<span class="hljs-value">"@dimen/activity_vertical_margin"</span>
    <span class="hljs-attribute">android:paddingLeft</span>=<span class="hljs-value">"@dimen/activity_horizontal_margin"</span>
    <span class="hljs-attribute">android:paddingRight</span>=<span class="hljs-value">"@dimen/activity_horizontal_margin"</span>
    <span class="hljs-attribute">android:paddingTop</span>=<span class="hljs-value">"@dimen/activity_vertical_margin"</span>
    <span class="hljs-attribute">tools:context</span>=<span class="hljs-value">".MainActivity"</span> ></span>
    <span class="hljs-tag"><<span class="hljs-title">EditText
</span>        <span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/etWord"</span>
        <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_content"</span>
        <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span>
        <span class="hljs-attribute">android:layout_alignParentLeft</span>=<span class="hljs-value">"true"</span>
        <span class="hljs-attribute">android:layout_alignParentTop</span>=<span class="hljs-value">"true"</span>
        <span class="hljs-attribute">android:layout_marginTop</span>=<span class="hljs-value">"27dp"</span>
        <span class="hljs-attribute">android:background</span>=<span class="hljs-value">"@android:drawable/edit_text"</span>
        <span class="hljs-attribute">android:ems</span>=<span class="hljs-value">"10"</span>
        <span class="hljs-attribute">android:singleLine</span>=<span class="hljs-value">"true"</span>
        <span class="hljs-attribute">android:textColor</span>=<span class="hljs-value">"#552006"</span>
        <span class="hljs-attribute">android:textColorHint</span>=<span class="hljs-value">"#782f10"</span> ></span>
        <span class="hljs-tag"><<span class="hljs-title">requestFocus</span> /></span>
    <span class="hljs-tag"></<span class="hljs-title">EditText</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">WebView
</span>        <span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/wvSearchResult"</span>
        <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span>
        <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span>
        <span class="hljs-attribute">android:layout_alignLeft</span>=<span class="hljs-value">"@+id/etWord"</span>
        <span class="hljs-attribute">android:layout_below</span>=<span class="hljs-value">"@+id/etWord"</span>
        <span class="hljs-attribute">android:layout_marginTop</span>=<span class="hljs-value">"22dp"</span>
        <span class="hljs-attribute">android:background</span>=<span class="hljs-value">"@drawable/bg_roundcorner"</span>
        <span class="hljs-attribute">android:textAppearance</span>=<span class="hljs-value">"?android:attr/textAppearanceMedium"</span>
        <span class="hljs-attribute">android:textSize</span>=<span class="hljs-value">"25sp"</span> /></span>

    <span class="hljs-tag"><<span class="hljs-title">Button
</span>        <span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/btnSearch"</span>
        <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_content"</span>
        <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span>
        <span class="hljs-attribute">android:layout_above</span>=<span class="hljs-value">"@+id/wvSearchResult"</span>
        <span class="hljs-attribute">android:layout_alignParentRight</span>=<span class="hljs-value">"true"</span>
        <span class="hljs-attribute">android:background</span>=<span class="hljs-value">"@drawable/ibsearchword"</span>
        <span class="hljs-attribute">android:onClick</span>=<span class="hljs-value">"searchWord"</span> /></span>

<span class="hljs-tag"></<span class="hljs-title">RelativeLayout</span>></span></code><ul style="display: block;" class="pre-numbering"><li></li></ul>

MainActivity.java

<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> com.example.youdaodictionary;

<span class="hljs-keyword">import</span> android.app.Activity;
<span class="hljs-keyword">import</span> android.os.Bundle;
<span class="hljs-keyword">import</span> android.text.TextUtils;
<span class="hljs-keyword">import</span> android.view.Menu;
<span class="hljs-keyword">import</span> android.view.View;
<span class="hljs-keyword">import</span> android.webkit.WebView;
<span class="hljs-keyword">import</span> android.webkit.WebViewClient;
<span class="hljs-keyword">import</span> android.widget.EditText;
<span class="hljs-keyword">import</span> android.widget.Toast;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span>
    <span class="hljs-keyword">private</span> EditText etWord;
    <span class="hljs-keyword">private</span> WebView wvResult;

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
    }

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initViews</span>() {
        etWord = (EditText) findViewById(R.id.etWord);
        wvResult = (WebView) findViewById(R.id.wvSearchResult);
        wvResult.setWebViewClient(<span class="hljs-keyword">new</span> WebViewClient() {
            <span class="hljs-annotation">@Override</span>
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">shouldOverrideUrlLoading</span>(WebView view, String url) {
                view.loadUrl(url);
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
            }
        });
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">onCreateOptionsMenu</span>(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">searchWord</span>(View view) {
        String word = etWord.getText().toString();
        <span class="hljs-keyword">if</span> (TextUtils.isEmpty(word)) {
            Toast.makeText(<span class="hljs-keyword">this</span>, <span class="hljs-string">"查询内容不能为空!"</span>, Toast.LENGTH_LONG).show();
        } <span class="hljs-keyword">else</span> {

            <span class="hljs-keyword">final</span> String strUrl = <span class="hljs-string">"http://dict.youdao.com/m/search?keyfrom=dict.mindex&vendor=&q="</span>
                    + word;
            wvResult.loadUrl(strUrl);
        }
    }
}
</code>

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

WebView调用有道词典实现在线查词

WebView(网络视图)能加载显示网页,可以将其视为一个浏览器。它使用了WebKit渲染引擎加载显示网页,使用方法很简单,直接在XML文件中写入webview控件即可,主要代码如下: <A...

使用android的webview控件访问有道词典的url

使用android的webview控件访问有道词典的url 思考: 1.我们要访问的url是什么2.通过什么访问3.访问完后怎样将它显示到我们的webview控件上去 实现: 1.URL是unifor...

练习:WebView控件使用(链接有道词典查单词)

练习:WebView控件使用(链接有道词典查单词)........

WebView控件应用——模仿URL有道词典

一、项目开发     1、开发介绍                本项目是用WebView控件模仿网上有道词典,We...

基于pygtk的linux有道词典

想把Linux用作桌面系统,其中一部分障碍就是Linux上没有像有道一样简单易用的词典。其实我们完全可以自己开发一款桌面词典,而且开发一款桌面词典也没用我们想象的那么难。在这门项目课中,我们就将开发一...

android笔记:简单的有道词典

有道词典运行效果图如下 一、布局文件  1、layout_main.xml     xmlns:tools="http://schemas.android.com/too...

安装有道词典Ubuntu16.04上

1. 平台: ubuntu 1604 LTS 2. 软件版本:youdao-dict_1.1.0-0-ubuntu_amd64.deb 3. 下载地址:http://cidian.youdao.c...

Python零基础入门二十一之有道词典的优化

上一篇博客简单的实现了有道词典的功能,这篇博客主要是对其功能进行进一步的优化。 之前的有道词典的小程序中存在着几个问题: (1)用户无法自定义输入的内容并且也无法多次输入,只能通过修改源代码来实现...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)