首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将html字符串传递给android上的webview

如何将html字符串传递给android上的webview
EN

Stack Overflow用户
提问于 2012-01-24 21:21:51
回答 4查看 159.7K关注 0票数 157

嗨,我正在解析xml,然后将其加载到web视图中,解析后我创建了四个字符串,以便我可以将所有字符串附加到一个视图中。我可以在web视图上获得两个视图,但不能获得前两个字符串。

请用我的代码建议我,我哪里出错了,什么是在web视图上获得格式化的html字符串的正确方法。请看一下我的代码,帮我解决这个问题。

代码语言:javascript
复制
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        String chapterTitle = "";
        String SubChapterTitle="";
        String chapterIntro ="";
        String chapterContent="";
        View view = convertView;
        if (convertView == null) {
            // view = inflater.inflate(resourceid, null);
            view = getLayoutInflater().inflate(R.layout.webviewitem, null);
        }
        synchronized (view) {
            WebView wv = (WebView) view.findViewById(R.id.contentWebView);

            WebSettings settings = wv.getSettings();
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            settings.setJavaScriptEnabled(true);
            settings.setDefaultZoom(ZoomDensity.FAR);
            // wv.setBackgroundColor(0);
            wv.setVerticalScrollBarEnabled(false);
            wv.setHorizontalScrollBarEnabled(false);
            /*String txtChapTitle = Intro.book.getsecretList().get(position)
                    .getChtitle().toString();*/

            if (!(Intro.book.getsecretList().get(position).getChtitle()
                    .toString().equals(""))){
            chapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
            .getChtitle().toString()+"</font></b>";
            }
            if (!(Intro.book.getsecretList().get(position)
                    .getSubtitle() == null)) {
                SubChapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
                .getSubtitle().toString()+"</font></b>";
            }
            if (!(Intro.book.getsecretList().get(position)
                    .getIntro() == null)) {
            chapterIntro = "<b><fontSize=2>"+Intro.book.getsecretList().get(position)
                .getIntro().toString()+"</font></b>";
            }
            if (!(Intro.book.getsecretList().get(position)
                    .getContent() == null)) {
            chapterContent = "<fontSize=2>"+Intro.book.getsecretList().get(position)
                .getContent().toString()+"</font>";
            }

            StringBuilder content = new StringBuilder();
            content.append(chapterTitle+SubChapterTitle+chapterIntro+chapterContent);

            JsInterface Jsi = new JsInterface();
            Jsi.wordDef = content ;
            Log.v("Content", "" +content);
            wv.addJavascriptInterface(Jsi, "interfaces");

            wv.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    view.setHapticFeedbackEnabled(false);
                }
            });

            wv.setWebChromeClient(new WebChromeClient() {
                @Override
                public boolean onJsAlert(WebView view, String url,
                        String message, JsResult result) {
                    return super.onJsAlert(view, url, message, result);
                }
            });

            wv.loadUrl("file:///android_asset/wordview.html");
        }
        return view;
    }
}

我可以得到chapterIntro和章节内容的网页视图,但不是前两个字符串,请帮助我的朋友。

EN

回答 4

Stack Overflow用户

发布于 2012-12-06 18:17:13

我已经成功地做到了下面这一点

代码语言:javascript
复制
 //data == html data which you want to load
 String data = "Your data which you want to load";

 WebView webview = (WebView)this.findViewById(R.id.webview);
 webview.getSettings().setJavaScriptEnabled(true);
 webview.loadData(data, "text/html; charset=utf-8", "UTF-8");

或者你也可以试试

代码语言:javascript
复制
 webview.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
票数 214
EN

Stack Overflow用户

发布于 2012-01-24 21:30:25

将数据加载到WebView中。调用WebView的loadData()方法

代码语言:javascript
复制
webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");

您可以查看此示例

http://developer.android.com/reference/android/webkit/WebView.html

票数 177
EN

Stack Overflow用户

发布于 2013-04-22 01:34:28

传递null会更好。完整的代码如下:

代码语言:javascript
复制
WebView wv = (WebView)this.findViewById(R.id.myWebView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL(null, "<html>...</html>", "text/html", "utf-8", null);
票数 41
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8987509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档