[英]How to get text from webview in android?
如何從android中的webview獲取文本? 這是我的一些代碼:
webView = (WebView)findViewById(R.id.wv_memo);
webView.requestFocus(View.FOCUS_DOWN);
webView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus())
{
v.requestFocus();
}
break;
}
return false;
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
webView.loadDataWithBaseURL(null,"<div contenteditable=\"true\" style=\"height:220px;\"></div>", "text/html", "utf-8", "about:blank");
您應該從此類中獲取Html標簽的值
class MyJavaScriptInterface
{
private Context ctx;
MyJavaScriptInterface(Context ctx)
{
this.ctx = ctx;
}
public void showHTML(String html)
{
String htmlTags = html;
}
}
在上面的代碼中,在showHTML()方法中,變量htmlTags具有所需的html全文。
@android.webkit.JavascriptInterface
沒有對添加的接口(MyJavaScriptInterface)中的方法進行注釋; 它們在API 17中不可見。出於安全原因,不應使用minSdkVersion <17調用WebView.addJavascriptInterface
可以使用反射來操縱應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.