我對android很新穎。現在我正在使用android html和java腳本功能。我試圖將數據從android頁面發送到html頁面,但我沒有成功。請告訴我如何在android中做到這一點。這對我有很大的幫助。如何在運行時從android中更改html頁面數據?
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腳本的頁面index.js
function myFunction()
{
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
在Android資產的文件夾,我把這些文件。在佈局中,我採取了一個編輯文本和一個按鈕。對於那個編輯文本,我得到了文本,我想將該文本更新到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;
}
}
}
當我點擊我的按鈕名稱將不會更新編輯文本。你能告訴我怎樣才能解決這個問題。
感謝 桑卡
嗨,當我點擊按鈕,我的職責是調用。但如果你介意我怎麼能改變HTML頁面中的文字。我必須發送自己的文本。那麼,我可以寫在js文件中。 – Nava2011
我不清楚我的建議是否對你有幫助。 '我的功能正在調用':如果你認爲你的功能現在被調用,那麼你怎麼知道? javascript函數'myFunction()'不帶參數,但用參數調用它。該如何工作?將其更改爲具有消息參數並將該消息作爲innerHTML。 – greenapps