当前位置: 移动技术网 > 移动技术>移动开发>Android > Android组件WebView编写有道词典小案例分享

Android组件WebView编写有道词典小案例分享

2019年07月24日  | 移动技术网移动技术  | 我要评论

最近学习了webview组件,写了一个有道词典的小案例,分享给大家,供大家参考,具体内容如下
效果图

源码下载https://coding.net/u/gxs1225/p/youdaodictionary/git

代码如下:
布局

activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/mainbg"
 android:paddingbottom="@dimen/activity_vertical_margin"
 android:paddingleft="@dimen/activity_horizontal_margin"
 android:paddingright="@dimen/activity_horizontal_margin"
 android:paddingtop="@dimen/activity_vertical_margin"
 tools:context=".mainactivity" >
 <edittext
  android:id="@+id/etword"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentleft="true"
  android:layout_alignparenttop="true"
  android:layout_margintop="27dp"
  android:background="@android:drawable/edit_text"
  android:ems="10"
  android:singleline="true"
  android:textcolor="#552006"
  android:textcolorhint="#782f10" >
  <requestfocus />
 </edittext>
 <webview
  android:id="@+id/wvsearchresult"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_alignleft="@+id/etword"
  android:layout_below="@+id/etword"
  android:layout_margintop="22dp"
  android:background="@drawable/bg_roundcorner"
  android:textappearance="?android:attr/textappearancemedium"
  android:textsize="25sp" />

 <button
  android:id="@+id/btnsearch"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_above="@+id/wvsearchresult"
  android:layout_alignparentright="true"
  android:background="@drawable/ibsearchword"
  android:onclick="searchword" />

</relativelayout>

mainactivity.java

package com.example.youdaodictionary;

import android.app.activity;
import android.os.bundle;
import android.text.textutils;
import android.view.menu;
import android.view.view;
import android.webkit.webview;
import android.webkit.webviewclient;
import android.widget.edittext;
import android.widget.toast;

public class mainactivity extends activity {
 private edittext etword;
 private webview wvresult;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  initviews();
 }

 private void initviews() {
  etword = (edittext) findviewbyid(r.id.etword);
  wvresult = (webview) findviewbyid(r.id.wvsearchresult);
  wvresult.setwebviewclient(new webviewclient() {
   @override
   public boolean shouldoverrideurlloading(webview view, string url) {
    view.loadurl(url);
    return true;
   }
  });
 }

 @override
 public boolean oncreateoptionsmenu(menu menu) {
  getmenuinflater().inflate(r.menu.main, menu);
  return true;
 }

 public void searchword(view view) {
  string word = etword.gettext().tostring();
  if (textutils.isempty(word)) {
   toast.maketext(this, "查询内容不能为空!", toast.length_long).show();
  } else {

   final string strurl = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&vendor=&q="
     + word;
   wvresult.loadurl(strurl);
  }
 }
}

更多精彩内容大家可以参考专题进行学习:

以上就是本文的全部内容,希望对大家学习android软件编程有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网