2012-09-21 111 views
0

我想在android webview中搜索並突出顯示一個單詞,我嘗試使用此代碼,但它不起作用。我的代碼是在這裏:搜索並突出顯示webview中顯示的html網頁中的單詞?

webview.findAll("a"); 
webview.setSelected(true); 
webview.findNext(true); 

try { 
    for (Method m : WebView.class.getDeclaredMethods()) { 
     if (m.getName().equals("setFindIsUp")) { 
      // m.setAccessible(true); 
      m.invoke(webview, true); 
      break; 
     } 
    } 
} catch (Exception ignored) 
{ 
    Log.i("highlight error", ignored.toString()); 
} 

這個代碼不設置任何亮點所選擇的單詞或不給任何錯誤,請告訴我如何搜索和網頁視圖中選擇一個的話,目前我試圖爲Android版本3.2?

+0

我試過每一個版本,它仍然沒有顯示突出顯示的文字 –

+0

任何合適的響應? –

+0

請給我一些建議... –

回答

1

在您的WebView佈局上設置搜索按鈕。 設置WebView,然後設置搜索按鈕,如下所示。彈出對話框中有一個取消按鈕,一個搜索按鈕和一個用於搜索詞的編輯文本。當按下搜索時,EditText字符串的每個匹配都將在WebView中突出顯示。

final Activity activity = this; 

    webView = (WebView) findViewById(R.id.yourWebView); 
    webView.setScrollbarFadingEnabled(false); 
    webView.setHorizontalScrollBarEnabled(false); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.clearCache(true); 

    webView.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
     progressDialog.setCanceledOnTouchOutside(true); 
     progressDialog.setTitle("Loading Web Page"); 
     progressDialog.setIcon(R.drawable.ic_menu_allfriends); 
     progressDialog.setButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
       webView.destroy(); 
       finish(); 
       } }); 
     progressDialog.show(); 
     progressDialog.setProgress(0); 
     activity.setProgress(progress * 1000); 
     progressDialog.incrementProgressBy(progress); 
     if(progress == 100 && progressDialog.isShowing()) 
       progressDialog.dismiss(); 
     } 
    }); 

    webView.loadUrl(yourStringURL); 

    Button search = (Button) findViewById(R.id.butSearch); 
    search.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
     //set up button for search keyword of the webView     
      final Dialog dSearch = new Dialog(myActivity.this); 
      dSearch.setContentView(R.layout.search_keyword); 
      dSearch.setCancelable(true); 
      dSearch.setTitle(getString(R.string.yourTitle)); 

      Button cancel = (Button) dSearch.findViewById(R.id.searchCancel); 
      cancel.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        dSearch.dismiss(); 
       } 
      }); 

      Button search = (Button) dSearch.findViewById(R.id.searchAdd); 
      search.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        EditText etkw = (EditText) dSearch.findViewById(R.id.searchword); 
        String keyword = etkw.getText().toString(); 
        dSearch.dismiss(); 
        webView.findAll(keyword); 
        try 
         { 
         Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE); 
         m.invoke(webView, true); 
         } 
        catch (Throwable ignored){} 
       } 
      }); 
      dSearch.show(); 
     } 
    }); 
+0

無法正常工作給出錯誤java.lang.NoSuchMethodException:setFindIsUp [boolean] –

+0

請檢查它是否在你身邊工作 –

+0

該代碼對我而言效果不錯 –

相關問題