欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android網(wǎng)易有道詞典案例源碼分享

 更新時間:2017年05月27日 10:56:51   作者:懷中貓  
這篇文章主要為大家分享了Android網(wǎng)易有道詞典案例源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、相關知識

SearchView控件:

以下是幾個簡單網(wǎng)址:SearchView簡單用法:

Android搜索框(SearchView)的功能和用法詳解

Android搜索框SearchView屬性和用法詳解

關于各種搜素: http://android.xsoftlab.net/guide/topics/search/index.html

SearchBar控件:大家還可以嘗試使用SearchBar控件

WebView控件: (巧妙使用該控件可以開發(fā)出很多有創(chuàng)意的應用,而且特別簡單)

二、實驗步驟

在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(){//如果不加此方法將會在瀏覽器中打開而不是運行的項目中重點內(nèi)容
   @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();
    //如果查詢內(nèi)容為空提示
    if (query.isEmpty())
    {
     Toast.makeText(getApplicationContext(), "查詢內(nèi)容不能為空!", Toast.LENGTH_SHORT)
       .show();
    }
    //否則則以參數(shù)的形式從http://dict.youdao.com/m取得數(shù)據(jù),加載到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);
 }

}

三、運行結果圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論