How to append additional text with existing html content in android?

烂漫一生 提交于 2020-01-11 04:14:13

问题


I am Developing an application.In which i am appending a text to existing text which is stored in .html file, the location for the .html file is in my application's "assets" folder. I know how to load html with URL but my problem is to append the text. following is my java code

public class LoadWeb extends Activity {
    WebView wv;
    private Handler handler = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        wv=(WebView)findViewById(R.id.webView1);
        handler = new Handler();  
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setBuiltInZoomControls(true);
        wv.addJavascriptInterface(this, "contactSupport");
        wv.getSettings().setSupportZoom(true);
        wv.setWebViewClient(new Callback());
       // String title=wv.getTitle();
    // Log.e("nitin", "msg"+title);


        setTheme(Color.CYAN);
       // wv.setInitialScale(100);




        wv.loadUrl("file:///android_asset/style.html");


        }
    void loadTime() {
        String TESTSTRING = "'<div style='color:blue; border:1px solid red;'>YO!</div>'"; 
        // TODO Auto-generated method stub
        Log.e("mitiiiiiii", ""+TESTSTRING);
        wv.loadUrl("javascript:appendText("+TESTSTRING+")"); 

    }


    private class Callback extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {


            return(true);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            loadTime();
            super.onPageFinished(view, url);
        }

    }
    public String deleteContact(final String contactId){  
        Toast.makeText(getBaseContext(),contactId ,Toast.LENGTH_LONG).show();
         handler.post(new Runnable() {  
              public void run() {  
                  Toast.makeText(getBaseContext(),contactId ,Toast.LENGTH_LONG).show();
              }  
            }); 

        Log.i("nitin"      ,""+contactId);
        return "http://google.com";
    }

}

and following is my javascript code

<html>
<head>
<script type="text/javascript">
var extraStr;
function appendText(extraStr) {

     document.getElementById('question')[0].innerHTML = document.getElementById('question')[0].innerHTML + extraStr;
     contactSupport.deleteContact();
 }  

function getUsed()
{
    var total = "";
    $(".boxWord.boxWordUsed").each(function (i){ total = total + this.getAttribute('index_no') + ":"; });
    return total;
}


</script>
<link href="style.css" media="all" rel="stylesheet" type="text/css" /> 
</head>
<body>
<div id="question" class='question' >
</div>

Can any body tell me why it is not working

Thanks in advance.


回答1:


Appending to HTML (concatenation) as String joining is not a good solution, but you can try this approach which will ensure that the html document's sanctity is not broken. Since you have the HTML document with you, it signifies you have control over that document and hence you should be able to do this which I am mentioning below.

1) Create a function in HTML

 function appendText(extraStr) {
     document.getElementsByTagName('body')[0].innerHTML =     document.getElementsByTagName('body')[0].innerHTML + extraStr;
 }  

2) On WebView load call this function

 myWebView.loadUrl("javascript:appendText('extra text or html here')"); 

3) Don't forget to add this

 myWebView.getSettings().setJavaScriptEnabled(true);


来源:https://stackoverflow.com/questions/7525196/how-to-append-additional-text-with-existing-html-content-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!