在使用锚链接加载html内容时,我在webview中遇到了一些奇怪的问题 以下代码适用于锚标记,但仅适用于 第二次当我按下锚标签时,无效。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mywebview);
String html = "<html><body><p><a href='#C4'>See also Chapter 4</a></p><p><h2>Chapter 1</h2><p>This chapter explains ba bla bla</p><h2>Chapter 2</h2><p>This chapter explains ba bla bla</p><h2>Chapter 3</h2><p>This chapter explains ba bla bla</p><h2><a name='C4'>Chapter 4</a></h2><p>This chapter explains ba bla bla</p><h2>Chapter 5</h2><p>This chapter explains ba bla bla</p><h2>Chapter 6</h2><p>This chapter explains ba bla bla</p><h2>Chapter 7</h2><p>This chapter explains ba bla bla</p><h2>Chapter 8</h2><p>This chapter explains ba bla bla</p><h2>Chapter 9</h2><p>This chapter explains ba bla bla</p></body></html>";
String mime = "text/html";
String encoding = "utf-8";
WebView myWebView = (WebView)this.findViewById(R.id.webView1);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);
}
我尝试了this链接,但没有为我工作。
更新
在各种设备中测试我的代码的结果。
工作
1. Samsung S Plus - &gt; 2.3.4
2.三星Galaxy Y - &gt; 2.3.6
3.三星Galaxy Tab - &gt; 2.3.3
4.摩托罗拉Xoom - &gt; 3.2
不工作
1. LG Optimus - &gt; 2.2
2.戴尔XCD35(中兴刀片) - &gt; 2.2
3. HTC WildFire - &gt; 2.2.1
这是操作系统问题还是其他问题?任何解决方案??
答案 0 :(得分:13)
问题是在锚点链接点击后再次重新加载页面。
我使用了以下代码,
Assets文件夹中的chapters.xml
<html>
<body>
<p><a href="#C4">See also Chapter 4</a></p>
<p><h2><a name='C1'>Chapter 1<a></h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 2</h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 3</h2><p>This chapter explains ba bla bla</p>
<h2><a name='C4'>Chapter 4</a></h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 5</h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 6</h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 7</h2><p>This chapter explains ba bla bla</p>
<a href="#C1">See also Chapter 1</a>
</body>
</html>
JAVA代码:第一种方式
public class MainActivity extends Activity {
WebView myWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myWebView = new WebView(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/chapters.html");
setContentView(myWebView);
final GestureDetector gestureDetector = new GestureDetector(
new MyGestureDetector());
View.OnTouchListener gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
myWebView.setOnTouchListener(gestureListener);
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
myWebView.reload();
Log.i("", "Reload");
return super.onSingleTapConfirmed(e);
}
}
}
编辑JAVA代码:第二种方式 - 我试过这个东西onTouchListener
,这对我来说很好。
public class MainActivity extends Activity {
WebView myWebView;
public static boolean flag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myWebView = new WebView(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/chapters.html");
setContentView(myWebView);
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
if (url.contains("#") && flag == false) {
myWebView.loadUrl(url);
flag = true;
} else {
flag = false;
}
}
});
}
}
从一个文件打开另一个HTML文件的锚点标记
myWebView = new WebView(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/1.htm");
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try{
chapter = url.substring(url.indexOf("#"),url.length());
url = url.substring(0,url.indexOf("#"));
}catch (Exception e) {
chapter = "";
}
myWebView.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (!chapter.equalsIgnoreCase("") && flag == false) {
myWebView.loadUrl(url+chapter);
flag = true;
} else {
flag = false;
}
}
});
setContentView(myWebView);
答案 1 :(得分:2)
请尝试使用此代码。它工作正常。
String html = "<html><body><p><a href='#C4'>See also Chapter 4</a></p><p><h2>Chapter 1</h2><p>This chapter explains ba bla bla</p><h2>Chapter 2</h2><p>This chapter explains ba bla bla</p><h2>Chapter 3</h2><p>This chapter explains ba bla bla</p><h2><a name='C4'>Chapter 4</a></h2><p>This chapter explains ba bla bla</p><h2>Chapter 5</h2><p>This chapter explains ba bla bla</p><h2>Chapter 6</h2><p>This chapter explains ba bla bla</p><h2>Chapter 7</h2><p>This chapter explains ba bla bla</p><h2>Chapter 8</h2><p>This chapter explains ba bla bla</p><h2>Chapter 9</h2><p>This chapter explains ba bla bla</p></body></html>";
String mime = "text/html";
String encoding = "utf-8";
final WebView myWebView = (WebView)this.findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL("app:html", html, mime, encoding, null);
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, "app:html");
view.loadDataWithBaseURL("app:html", html, mime, encoding, null);
}
});
答案 2 :(得分:0)
在Android 4.0.3和4.0.4设备上会出现此问题:如果您有一个移动网页(在资源中或从网络加载),其内容填满整个屏幕宽度,那么有些链接如下: / p>
<a href=... HTML links
......不行!他们只是无法加载下一页!这有点奇怪。
此处记录了错误:http://code.google.com/p/android/issues/detail?id=929
即使它是在2009年登录的,它仍然会在Android 4.0.3 / 4.0.4设备上发生。
解决方案是不使用:
WebView上的 android:layout_height="wrap_content
。
将其替换为:
android:layout_height="match_parent
答案 3 :(得分:0)
试试这个简单的2线程javascript
<html>
<body>
<head>
<script language="javascript">
function jump_top() {
location.href = "#top";
location.hash = "";
}
</script>
</head>
<body>
<a name="top"></a>
<br /><br /><br /><br />
...
<br />Now you are at the bottom of the page
<br />
<a onclick="jump_top()">To Top</a>
</body>
</html>