如何追加,在安卓现有的HTML内容的附加文本?文本、内容、HTML

2023-09-07 02:32:17 作者:动了情的痞子

我开发一个application.In对此我追加文本到现有的存储在.html文件中的文本,为.html文件的位置是在我的应用程序的资产文件夹中。我知道如何加载HTML与URL,但我的问题是要追加的文本。 以下是我的Java code

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

}

和以下是我的javascript code

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

在此先感谢。

推荐答案

追加到HTML(串联)作为字符串连接并不是一个很好的解决方案,但你可以试试这个方法,这将确保HTML文档的神圣不破。既然你的HTML文档和你在一起,这意味着你必须在该文档的控制,因此,你应该能够做到这一点,我很提的下面。

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)在HTML中创建一个函数

1) Create a function in HTML

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

2)的WebView加载调用这个函数

2) On WebView load call this function

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

3)不要忘了添加此

3) Don't forget to add this

 myWebView.getSettings().setJavaScriptEnabled(true);
 
精彩推荐
图片推荐