如何在android上将html字符串传递给webview [英] How to pass html string to webview on android

查看:23
本文介绍了如何在android上将html字符串传递给webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在解析 xml,然后将其加载到 Web 视图,解析后我正在创建四个字符串,以便可以将所有字符串附加到一个视图.我能够在 Web 视图上获得两个视图,但不能获得前两个字符串.

Hi I am parsing xml and then loading it to web view, after parsing I am creating four strings so that I could append all string to one view. I am able to get two views on the web view but not the first two strings.

请用我的代码建议我,我哪里出错了,以及在 Web 视图上获取格式化的 html 字符串的正确方法是什么.请查看我的代码并帮助我解决此问题.

Pls suggest me with my code, where am I going wrong and what is the correct way to get the formatted html strings on the web view. Pls have a look at my code and help me solve this issue.

@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;
    }
}

我可以在 web 视图上获取chapterIntro 和chaptercontent,但前两个字符串不能,请帮助我的朋友.

I am able to get chapterIntro and chaptercontent on the web view but not the first two strings pls help me friends.

推荐答案

我已经成功通过下面这行

i have successfully done by below line

 //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");

或者你可以试试

 webview.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);

这篇关于如何在android上将html字符串传递给webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆