欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android网易有道词典案例源码分享

程序员文章站 2023-12-02 19:28:22
一、相关知识 searchview控件: 以下是几个简单网址:searchview简单用法: android搜索框(searchview)的功能和用法详解 andr...

一、相关知识

searchview控件:

以下是几个简单网址:searchview简单用法:

android搜索框(searchview)的功能和用法详解

android搜索框searchview属性和用法详解

关于各种搜素:

searchbar控件:大家还可以尝试使用searchbar控件

webview控件: (巧妙使用该控件可以开发出很多有创意的应用,而且特别简单)

二、实验步骤

在activity_main xml中

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <!-- 顶一个searchview -->
 <searchview
  android:id="@+id/sv"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
 <!-- 建立一個webview -->
 <webview
 android:id="@+id/wv"
 android:layout_height="match_parent"
 android:layout_width="match_parent"
 android:background="@android:color/black"
 android:focusable="false"
  />
</linearlayout>

在mainactivity中

package bzu.edu.cn.happydirectory;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.webkit.webview;
import android.webkit.webviewclient;
import android.widget.searchview;
import android.widget.toast;
public class mainactivity extends appcompatactivity {
 private static searchview searchview;
 private static webview webview;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  init();
  searchview.setsubmitbuttonenabled(true);/// 设置该searchview显示确认搜索按钮
  webview.getsettings().setjavascriptenabled(true);//如果页面中使用了javascript,不加代码页面不显示
  webview.setwebviewclient(new webviewclient(){//如果不加此方法将会在浏览器中打开而不是运行的项目中重点内容
   @override
   public boolean shouldoverrideurlloading(webview view, string url) {
    view.loadurl(url);
    return true;
   }
  });

  searchview.setonquerytextlistener(new searchview.onquerytextlistener() {
   @override
   public boolean onquerytextsubmit(string query) {
    string struri = (query);
    struri = struri.trim();
    //如果查询内容为空提示
    if (query.isempty())
    {
     toast.maketext(getapplicationcontext(), "查询内容不能为空!", toast.length_short)
       .show();
    }
    //否则则以参数的形式从http://dict.youdao.com/m取得数据,加载到webview里.
    else
    {
     string strurl = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q="
       + struri;
     webview.loadurl(strurl);
    }
    return false;
   }

   @override
   public boolean onquerytextchange(string newtext) {
    return false;
   }
  });
 }
 public void init(){
  searchview =(searchview)findviewbyid(r.id.sv);
  webview =(webview)findviewbyid(r.id.wv);
 }

}

三、运行结果图

Android网易有道词典案例源码分享

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。