我用anchor link加載html內容時,在webview中遇到一些奇怪的問題。
以下代碼對於 anchor標簽可以很好的執行,但是只能執行一次。
當我再次點擊 anchor標簽時,就不能執行。
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);
}
我在不同的設備裝置中測試代碼。
可以執行:
1. Samsung S Plus --> 2.3.4
2. Samsung Galaxy Y --> 2.3.6
3. Samsung Galaxy Tab --> 2.3.3
4. Motorola Xoom --> 3.2
不能執行:
1. LG Optimus --> 2.2
2. DELL XCD35(ZTE Blade) --> 2.2
3. HTC WildFire --> 2.2.1
這是操作系統的問題還是別的問題? 如何解決呢?
問題是想點擊Anchor鏈接後重新加載頁面。
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;
}
}
});
}
}