Android之WebView的使用

package com.twkj.demo.activity;

import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

	private WebView webView;
	String url="file:///android_asset/index.html";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=(WebView) findViewById(R.id.webview);
        //webview支持js
        WebSettings settings= webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new WebAppInterface(MainActivity.this), "Android");
        webView.loadUrl(url);     
        webView.setWebViewClient(new WebViewClient(){       	
        	@Override
        	public boolean shouldOverrideUrlLoading(WebView view, String url) {
        		// TODO Auto-generated method stub
        		view.loadUrl(url);
        		return super.shouldOverrideUrlLoading(view, url);
        	}       	
        });
        webView.setWebChromeClient(new WebChromeClient());
    }
    
    //这个类相当于连接js和Activity的桥梁
    class WebAppInterface{
    	
    	private Context c;

		public WebAppInterface(Context c) {
			super();
			this.c = c;
		}
		//显示提示信息的方法
		public void showToastInfo(String msg){	
			Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
		}
		public String checkUser(String name,String password){
			if(name.equals("111")&&password.equals("111")){
		//		webView.loadUrl("javascript:intent('success.html')");
				return "1";
			}else{
				Toast.makeText(c, "用户名或者密码不正确", Toast.LENGTH_SHORT).show();
				return "0";
			}
		}
    }
    
    //点击返回键,让webview返回上一级
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	// TODO Auto-generated method stub
    	if(keyCode==KeyEvent.KEYCODE_BACK&&webView.canGoBack()){
    		webView.goBack();
    		return true;
    	}
    	return super.onKeyDown(keyCode, event);
    	
    }
}

index.html

<html>
<head>
<title>index</title>
<meta charset="utf-8">
<style type="text/css">

#login{
      width: 100px;
      height: 38px;
      background: #ff0038;
}
body{
   background: pink;
}
</style>
</head>
<script type="text/javascript"></script>
<script>  
   function demo(){
	   //获得用户名
	   var name=document.getElementById("name").value;
	   var password=document.getElementById("password").value;
	   if(name.length==0||password.length==0){
		   //弹出用户的提示
		   Android.showToastInfo("用户名或者密码为空");
	   }else{
		 var result= Android.checkUser(name,password);
		 if(result==1){
			 window.location="success.html";
		 }
	   }  
   }
   function intent(url){
	   window.location=url;
   }
   
</script>

<body>
	<center><h2>用户登录</h2></center>
	<form>
		<table border="1">
			<tr>
				<td>用户姓名</td>
				<td><input type="text" id="name" /></td>
			</tr>
			<tr>
				<td>用户密码</td>
				<td><input type="password" id="password" /></td>
			</tr>
			<tr align="center">
				<td colspan="2"><input type="button" value="登录" id="login" onclick="demo();"></td>
			</tr>
		</table>
	</form>
	
</body>
</html>


你可能感兴趣的:(Android之WebView的使用)