我只想问一个简单的问题。
我有这个我下载的 html 页面(及其文件)并将其放置在 assets 文件夹中并将其加载到 webview 中,提交时网页发送填充文本字段的数据,我的问题是我可以检测到 onSubmit 点击,和如果可以,我可以检索发送的数据,以便在没有互联网的情况下将它们保存在 sqlite 中,并在连接应用程序时发送它们。
问候,
这是你可以做的!希望这有意义
考虑在您的资产文件夹中,您有一个 html 文件,例如
/assets/mypage.html
其中有一些像这样的js
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>My HTML</title>
</head>
<body>
<h1>MyHTML</h1>
<p id="mytext">Hello!</p>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<input type="button" value="Open Dialog" onClick="openAndroidDialog()" />
<script language="javascript">
function showAndroidToast(toast) {
AndroidFunction.showToast(toast);
}
function openAndroidDialog() {
AndroidFunction.openAndroidDialog();
}
function callFromActivity(msg){
document.getElementById("mytext").innerHTML = msg;
}
</script>
</body>
</html>
你的xml设计对于java的app Activity是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/sendmsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Msg to JavaScript"
/>
<WebView
android:id="@+id/mybrowser"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
现在在您的 MainActivity.java 类中,我在其中为这样的侦听器使用了一个接口
package com.rizwan.AndroidHTML;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AndroidHTMLActivity extends Activity {
WebView myBrowser;
EditText edtTxtMsg;
Button btnSendMsg;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBrowser = (WebView)findViewById(R.id.mybrowser);
final MyJavaScriptInterface myJavaScriptInterface
= new MyJavaScriptInterface(this);
myBrowser.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.loadUrl("file:///android_asset/mypage.html");
edtTxtMsg = (EditText)findViewById(R.id.msg);
btnSendMsg = (Button)findViewById(R.id.sendmsg);
btnSendMsg.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String msgToSend = edtTxtMsg.getText().toString();
myBrowser.loadUrl("javascript:callFromActivity(\""+msgToSend+"\")");
}});
}
public class MyJavaScriptInterface {
Context mContext;
MyJavaScriptInterface(Context c) {
mContext = c;
}
public void showToast(String toast){
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
public void openAndroidDialog(){
AlertDialog.Builder myDialog
= new AlertDialog.Builder(AndroidHTMLActivity.this);
myDialog.setTitle("DANGER!");
myDialog.setMessage("You can do what you want!");
myDialog.setPositiveButton("ON", null);
myDialog.show();
}
}
}