在 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();
}
});