2 o casper ne O_Casper_ne 于 2012.12.13 16:36 提问

在webview中Html Anchor Link只能执行一次

我用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

这是操作系统的问题还是别的问题? 如何解决呢?

2个回答

nana4ever
nana4ever   2013.01.08 13:19
已采纳

问题是想点击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;
                }
            }

        });
    }
}
CSDN_Ask
CSDN_Ask   2012.12.24 11:21
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);
       }  
    });

使用这段代码,能实现你想要的功能。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!