关闭

有道词典Demo(WebView)

142人阅读 评论(0) 收藏 举报

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

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>

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1132次
    • 积分:88
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档