dd

WebView JS 交互

jerry Android 2015年11月23日 收藏

WebView加jquery做页面会怎么样呢?

java代码:

public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";

	// 定义访问的链接
	private static String URL = "file:///android_asset/index.html";

	// 定义WebView
	WebView mWebView = null;

	// 定义数据列表
	private List<Program> programList = null;
	private static final int PROGRAM_NUM = 25;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 在标题栏上显示进度
		getWindow().requestFeature(Window.FEATURE_PROGRESS);

		// 创建WebView对象
		mWebView = new WebView(this);
		setContentView(mWebView);

		final Activity activity = this;
		// 在标题栏显示进度
		mWebView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				activity.setProgress(progress * 100);
			}
		});

		programList = initProgramList();

		// 设置JS可用
		mWebView.getSettings().setJavaScriptEnabled(true);
		// 把programList添加到js的全局对象window中,
		// 这样就可以使用window.programList来获取数据
		mWebView.addJavascriptInterface(programList, "programList");
		mWebView.addJavascriptInterface(this, "MainActivity");

		// 加载页面
		mWebView.loadUrl(URL);
	}

	/*
	 * 定义js回调java函数
	 */
	public void showSelect(int id) {
		Log.i(TAG, "select:" + id);
	}

	// 绑定键盘的向上,向下按钮事件触发相应的js事件
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			mWebView.loadUrl("javascript:moveUp()");
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			mWebView.loadUrl("javascript:moveDown()");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	// 初始化数据
	private List initProgramList() {
		List<Program> list = new ArrayList<Program>();
		for (int i = 1; i <= PROGRAM_NUM; i++) {
			Program p = new Program(i, "Program:::" + i);
			list.add(p);
		}
		return list;
	}

	/*
	 * 定义简单的数据类
	 */
	class Program {
		private int id;
		private String name;

		public Program(int id, String name) {
			this.id = id;
			this.name = name;
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

	}

对应的html文件:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.js"></script>
<script>
	var startTop = 120;
	var startPos = 1;
	var totalNum = 0;
	var itemHeight = 40;
	$(document).ready(function(){
		var con = '';
		if(window.programList){
			//初始化页面
			totalNum = window.programList.size();
			for(i=0;i< totalNum;i++){
				con+='<div class="item" tar="'+programList.get(i).getId()+'">'+programList.get(i).getName()+'</div>';
			}
		}
		
		$('#list').html(con);
		//绑定事件
		$('.item').click(function(){
			var tar = $(this).attr('tar');
			//回调java事件
			window.MainActivity.showSelect(tar);
		});
	});
	
	//接收键盘向下事件
	function moveDown(){
		if(startPos < totalNum){
			startPos ++;
			var top = (1 - startPos)* itemHeight + startTop;
			$('#list').stop().animate({top:top});
		}
	}
	//接收键盘向上事件
	function moveUp(){
		if(startPos > 1){
			startPos --;
			var top = (1 - startPos )* itemHeight + startTop;
			$('#list').stop().animate({top:top});
		}
	}
</script>
<style>
	.box{
		position:relative;
		left:20px;
		top:20px;
		width:200px;
		height:280px;
		background-color:#CCC;
		overflow:hidden;
	}
	
	.list{
		position:absolute;
		left:10px;
		top:120px;
		width:180px;
	}
	
	.item{
		height:40px;
		line-height:40px;
	}
	
	.select{
		position:absolute;
		left:4px;
		top:120px;
		border:1px solid #F00;
		height:40px;
		width:190px;
	}
</style>
</head>
<body >
	
	<div class="box">
    	<div class="list" id="list">
        	
        </div>
        <div class="select">&nbsp;</div>
    </div>
</body>
</html>
dd