//网页XML代码
<html>
<head>
<script>
//给函数一个返回值
function to(){
alert("起飞")
}
function val(){
var vname=document.getElementById("uname").value; //获取文本框中的值
var vpwd=document.getElementById("upwd").value; //获取密码文本框中的值===>赋值给变量vpwd
if(vname==""){
alert("用户名不能为空!");
return false;
}else if(vpwd==""){
alert("密码不能为空!");
return false;
}
//把值给android应用程序
meet.getjs(vname,vpwd);
return false;
}
</script>
</head>
<body>
<form action="welcome.html" οnsubmit="return val()">
用户名<input id="uname" name="uname" /><br/>
密码<input id="upwd" type="password" /><br/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
//布局文件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
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=".MainActivity"
android:orientation="vertical" >
<Button
android:onClick="btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调javascript"/>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/wv" />
</LinearLayout>
//main方法
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv=(WebView) findViewById(R.id.wv);
WebSettings settings = wv.getSettings();
settings.setDefaultTextEncodingName("utf-8");
settings.setJavaScriptEnabled(true);
//允许弹框
wv.setWebChromeClient(new WebChromeClient(){});
//不使用系统的浏览器打开
wv.setWebViewClient(new WebViewClient(){});
wv.addJavascriptInterface(new chuan(), "meet");
wv.loadUrl("file:///android_asset/aa.html");
}
//调网页的方法
public void btn(View v){
wv.loadUrl("javascript:to()");
}
class chuan{
@JavascriptInterface
public void getjs(String name){
Toast.makeText(MainActivity.this, "欢迎"+name, 1).show();
}
}
}