在加载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); }
更新:
测试我的代码在各种设备中的结果.
加工
三星S Plus – > 2.3.4
三星Galaxy Y – > 2.3.6
三星Galaxy Tab – > 2.3.3
摩托罗拉Xoom – > 3.2
不工作
LG Optimus – > 2.2
DELL XCD35(中兴刀片) – > 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代码:第二种方式 – 我已经尝试了这个东西instt 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; } } }); } }
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);