How to append additional text with existing html c

2020-03-08 08:01发布

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条回答
家丑人穷心不美
2楼-- · 2020-03-08 08:40

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);
查看更多
登录 后发表回答