当前位置: 代码迷 >> Web前端 >> android > WebView > 获取表奇数据
  详细解决方案

android > WebView > 获取表奇数据

热度:974   发布时间:2012-12-15 15:16:03.0
android > WebView > 获取表单数据

?

先是 HTML

?

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<script type="text/javascript" src="./jquery_1.4.min.js"></script>
	<script language="javascript">
	<!--
	
    $(document).ready(function(){
       $.extend({
			getValue:function(inputName){
				var str = document.getElementById(inputName).value;
				window.myjs.runOnAndroidJavaScript(str);//调用android的函数
			}
		});		
    });
	
	//-->
	</script>
</head>
<body style="padding-top:10px">
<input type="number" name=""  id="time_1" >
</body>
</html>
?

?

?

?

然后是.java

?

public class ContrlSetWater extends Activity{
	
	private ImageButton back;
	private WebView wb;
	
	
	private Handler h = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contrl_set_water);	
		back = (ImageButton)findViewById(R.id.back);
		wb = (WebView)findViewById(R.id.wb);
		

    	
		//webview
		wb.setBackgroundColor(Color.parseColor("#1A1A1A"));
		wb.getSettings().setJavaScriptEnabled(true);  
		wb.setHorizontalScrollBarEnabled(false);//水平不显示   
		wb.setVerticalScrollBarEnabled(false); //垂直不显示
		wb.loadUrl("file:///android_asset/set_water.html"); 
		
		wb.setInitialScale(95);	//800

		wb.getSettings().setSupportZoom( true ); //Modify this
		wb.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);//Add this
		
		wb.addJavascriptInterface(new runJavaScript(), "myjs");	////----自己定义的myjs 在 HTML 中使用
		//\\
		back.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				wb.loadUrl("javascript:$.getValue('time_1')"); 
			}
		}); 		
		//\\
	    
	}
	
	final class runJavaScript{//这个Java 对象是绑定在另一个线程里的,
		public void runOnAndroidJavaScript(final String str){
			h.post(new Runnable(){
				public void run() {//这里应该特别注意的
					 
					Toast.makeText(ContrlSetWater.this, "out:"+str, Toast.LENGTH_LONG).show();
				}
	    
			});
	      }
	  }
	
	
}
?

?

解决缓存问题 ,在最开始 使用以下代码

wb.clearCache(true);

wb.clearHistory();

wb.clearView();

wb.loadDataWithBaseURL(null, "","text/html", "utf-8",null);

wb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存

.........

........

....

?

  相关解决方案