我怎么能改变从android在运行时的html页面数据

How can i change the html page data from android in the run time?

本文关键字:html 数据 运行时 能改变 android      更新时间:2023-09-26

我是android的新手。现在我正在工作的android html和java脚本功能。我曾试图将数据从android页面发送到html页面,但我没有成功。请告诉我如何在android中做到这一点。这对我帮助很大。

Html页面:

<!doctype html>
        <html lang="en">
         <head>
          <meta charset="UTF-8">
          <meta name="Generator" content="EditPlus®">
          <meta name="Author" content="">
          <meta name="Keywords" content="">
          <meta name="Description" content="">
          <title>Document</title>
    <script src="index.js"></script>
         </head>
         <body>
          <h1>This is my first webpage</h1>
          <p id="demo">A Paragraph.</p>
        <button type="button" onclick="myFunction()">Try it</button>

         </body>
        </html>

Java script page index.js

 function myFunction() 
 {
document.getElementById("demo").innerHTML = "Paragraph changed.";
 }

我把这些文件放在android assets文件夹中。在布局我已经采取了一个编辑文本和一个按钮。对于编辑文本,我得到了文本,我想更新的文本到html页面。这就是任务。所以我选择了

布局文件:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="100"
        android:orientation="horizontal" >
        <EditText 
            android:id="@+id/edittext"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="70"/>
        <Button 
            android:layout_width="0dp"
            android:layout_weight="30"
            android:id="@+id/button"
            android:layout_height="40dp"
            android:text="Button"/>
    </LinearLayout>
    <WebView
        android:id="@+id/graph"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
活动:

package com.roopasoft.drawtriangle;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class HtmlLoad extends Activity implements OnClickListener{
EditText editText;
Button button;
WebView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    view = (WebView)findViewById(R.id.graph);
    editText = (EditText)findViewById(R.id.edittext);
    button = (Button)findViewById(R.id.button);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl("file:///android_asset/TextDemo.html");
    button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button:
        String name = editText.getText().toString();
        view.loadUrl("file:///android_asset/javascript:myFunction('""+name+"'")");
        break;
    }
}

}

当我点击我的按钮,名称将不会更新编辑文本。你能告诉我如何解决那个问题吗?

谢谢Shankar

变化

view.loadUrl("file:///android_asset/javascript:myFunction('""+name+"'")");

view.loadUrl("javascript:myFunction('""+name+"'")");

很久以前,我在webview上加载了html,并从android的javascript中轮询数据。像这样。

WebView wbview = (WebView) this.findViewById(R.id.report_wv_chartview);
WebSettings webSettings = wbview.getSettings();
webSettings.setJavaScriptEnabled(true);
wbview.addJavascriptInterface(this, "webConnector");
wbview.addJavascriptInterface(this, "toaster");
webSettings.setBuiltInZoomControls(true);
wbview.requestFocusFromTouch();
wbview.setWebViewClient(new WebViewClient());
wbview.setWebChromeClient(new WebChromeClient());
wbview.loadUrl("file:///android_asset/html/bar.html");

public String get_bar_json()
{
    return jString;
}

然后在HTML代码中:

// i used jquery
<script id="source" language="javascript" type="text/javascript">
    $(function()
    {
    var jsonData = new Object();
    var jsonData = window.webConnector.get_bar_json();
    .
    .
    .
    });
</script>