有道词典Demo(WebView)

学习了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>

 

阅读更多
上一篇Android用户自动登录
下一篇联网查看图片的Demo
想对作者说点什么? 我来说一句

安卓webview与js交互demo

2015年11月19日 1.19MB 下载

android webview demo

2016年01月20日 6.99MB 下载

有道和百度翻译demo

2013年12月17日 60KB 下载

Android混合开发之webviewdemo

2017年01月12日 3.3MB 下载

webview demo 可以和js交互,亲测可用

2018年07月16日 19.73MB 下载

IOS UIWebView Demo

2012年06月25日 26KB 下载

WebView写的浏览器Demo

2016年07月20日 2.14MB 下载

没有更多推荐了,返回首页

关闭
关闭