从Android的web视图的html code [英] android html code from webview
问题描述
你好,我是用这个例子的http:// lexandera。 COM / 2009/01 /解压-HTML-从-A-web视图/ ,以获得一个网页视图的HTML。但我需要在我超使用它,我不知道如何做到这一点。我可以看到在AlertDialog的HTML,但我不能使用它。我怎么能回到我的主类为String?
最后语境对myApp =这一点;/ *这个类的一个实例将被注册为一个JavaScript接口* /
类MyJavaScriptInterface
{
@燮pressWarnings(未使用)
公共无效showHTML(字符串HTML)
{
新AlertDialog.Builder(对myApp)
.setTitle(HTML)
.setMessage(HTML)
.setPositiveButton(android.R.string.ok,NULL)
.setCancelable(假)
。创建()
。显示();
}
}最终的WebView浏览器=(的WebView)findViewById(R.id.browser);
如果你想要的工作,显然/ *必须启用JavaScript * /
browser.getSettings()setJavaScriptEnabled(真)。/ *注册名为HTMLOUT *一个新的JavaScript接口/
browser.addJavascriptInterface(新MyJavaScriptInterface(),HTMLOUT);/ * WebViewClient必须设置前调用使用loadURL! * /
browser.setWebViewClient(新WebViewClient(){
@覆盖
公共无效onPageFinished(的WebView视图,字符串URL)
{
/ *这个调用JavaScript插入刚刚完成加载的页面。 * /
browser.loadUrl(\"javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');\");
}
});/ *加载网页* /
browser.loadUrl(http://lexandera.com/files/jsexamples/gethtml.html);
方法如下:
设置的WebView
首先添加JavaScriptInterface到web视图如下。这里的Android稍后将用于以后调用函数JavaScriptInterface。
web视图=(的WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(真);
webView.addJavascriptInterface(新JavaScriptInterface(本),MyAndroid);
该JavaScriptInterface类:
公共类JavaScriptInterface {
上下文mContext;
JavaScriptInterface(上下文C){
mContext = C;
}
}
HTML文件:
&LT; HTML和GT;
&LT; HEAD&GT;
&LT;脚本类型=文/ JavaScript的&GT;
功能的getValue()
{
VAR X =的document.getElementById(内容)的innerHTML。
//警报(X);
MyAndroid.receiveValueFromJs(X);
}
&LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
&LT; DIV ID =内容&GT;
这是HTML内容和LT;小时/&GT;
的其他内容;
&LT;输入类型=按钮的onclick =的getValue()VALUE =获取内容/&GT;
&LT; / DIV&GT;
&LT; /身体GT;
&LT; / HTML&GT;
中调用Java脚本函数
//调用这个javascript函数
webView.loadUrl(JavaScript的:的getValue());
添加回调函数JavaScriptInterface为MyAndroid.receiveValueFromJs(VAL):
公共无效receiveValueFromJs(字符串str){
Toast.makeText(mContext,接收到的值由JS:+ STR,Toast.LENGTH_SHORT).show();
}
在这里,你在str变量返回的HTML。
您可以看到我的博客:的http://ganeshtiwaridotcomdotnp.blogspot.com/2011/10/calling-javascript-function-from.html
了解详情。
我找到了另一种解决办法:<一href=\"http://stackoverflow.com/questions/3479833/is-it-possible-to-get-the-html-$c$c-from-webview/3482047#3482047\">Is有可能得到的WebView的HTML code
hello i'm using this example http://lexandera.com/2009/01/extracting-html-from-a-webview/ to get the HTML from a webview. But i need to use it in my superclass and i dont know how to do this. I just can see the html on a AlertDialog but i cant use it. How can I return it to my main class as String?
final Context myApp = this;
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
@SuppressWarnings("unused")
public void showHTML(String html)
{
new AlertDialog.Builder(myApp)
.setTitle("HTML")
.setMessage(html)
.setPositiveButton(android.R.string.ok, null)
.setCancelable(false)
.create()
.show();
}
}
final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);
/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
browser.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
/* load a web page */
browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");
Do the Followings :
Setting up WebView
First add JavaScriptInterface to webView as follows. Here "Android" will be used later to call functions in JavaScriptInterface later.
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JavaScriptInterface(this), "MyAndroid");
The JavaScriptInterface Class:
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
}
HTML File:
<html>
<head>
<script type="text/javascript">
function getValue()
{
var x=document.getElementById("content").innerHTML;
// alert(x);
MyAndroid.receiveValueFromJs(x);
}
</script>
</head>
<body>
<div id="content">
This is html content <hr/>
Other contents;
<input type="button" onclick="getValue()" value="Get Content" />
</div>
</body>
</html>
Calling Java Script Function
//this calls javascript function
webView.loadUrl("javascript:getValue()");
Adding Callback function in JavaScriptInterface for MyAndroid.receiveValueFromJs(val):
public void receiveValueFromJs(String str) {
Toast.makeText(mContext, "Received Value from JS: " + str,Toast.LENGTH_SHORT).show();
}
Here you have returned HTML in str variable.
You can see my blog : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/10/calling-javascript-function-from.html for details.
I found another solution : Is it possible to get the HTML code from WebView
这篇关于从Android的web视图的html code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!