简介
本文旨在大致介绍Android和JS的交互,并进行了实现。
废话不说直接上代码:
public class MainActivity extends AppCompatActivity{
WebView mwebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mwebview = (WebView) findViewById(R.id.mwebview);
mwebview.getSettings().setJavaScriptEnabled(true);
mwebview.loadUrl("file:///android_asset/web.html");
mwebview.addJavascriptInterface(MainActivity.this,"android");
mwebview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mwebview.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}
@JavascriptInterface
public void call_Android(){
runOnUiThread(new Runnable() {
@Override
public void run() {
startActivityForResult(new Intent(MainActivity.this,LoginActivity.class),100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode){
case 1000:
String data2 = "我是孩子的ID";
mwebview.loadUrl("javascript:postInfo('" + data2 + "')");
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bsoft.android_js.MainActivity">
<WebView
android:id="@+id/mwebview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"/>
</LinearLayout>
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btn_back = (Button) findViewById(R.id.btn_back);
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(1000);
finish();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.bsoft.android_js.LoginActivity">
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20dp"
android:text="登陆"/>
</android.support.design.widget.CoordinatorLayout>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<script type="text/javascript">
<!--此处是接收就诊人信息-->
function postInfo(arg){
document.getElementById("content").innerHTML =
("<br\>"+arg);
}
</script>
</head>
<body>
HTML 内容显示 <br/>
<h1><div id="content">内容显示</div></h1>
<br/>
<!--此处调用Android方法-->
<input type="button" value="预约(检查并跳转导登陆)" onclick="window.android.call_Android()" />
</body>
</html>
网友评论