編輯:關於Android編程
[java]
package cn.testjavascript;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;
/**
* Demo描述:
* 在Android中獲取網頁裡表單中的數據
*/
public class MainActivity extends Activity {
private WebView mWebView;
private String date =null;
private String email = null;
private String username = null;
private String sex = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mWebView=(WebView) findViewById(R.id.webView);
initWebViewSettings();
mWebView.loadUrl("file:///android_asset/form.html");
//注意addJavascriptInterface方法中第二參數
//它表示我們的java對象javaClass的別名.
//這樣Javascript就可以通過該別名來調用Android中的方法
//即Javascript代碼中的:window.testform.send(date+"|"+email+"|"+name+"|"+sex);
//send是方法名
//testform是別名
mWebView.addJavascriptInterface(new Object() {
public void send(String userInfo) {
StringTokenizer userInfoStringTokenizer = new StringTokenizer(userInfo, "|");
date = userInfoStringTokenizer.nextToken();
email = userInfoStringTokenizer.nextToken();
username = userInfoStringTokenizer.nextToken();
sex = userInfoStringTokenizer.nextToken();
System.out.println("userInfoStringTokenizer="+userInfoStringTokenizer.toString());
System.out.println("date=" + date);
System.out.println("email=" + email);
System.out.println("username=" + username);
System.out.println("sex=" + sex);
};
}, "testform");
}
private void initWebViewSettings(){
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.requestFocus();
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
}
}
package cn.testjavascript;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;
/**
* Demo描述:
* 在Android中獲取網頁裡表單中的數據
*/
public class MainActivity extends Activity {
private WebView mWebView;
private String date =null;
private String email = null;
private String username = null;
private String sex = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mWebView=(WebView) findViewById(R.id.webView);
initWebViewSettings();
mWebView.loadUrl("file:///android_asset/form.html");
//注意addJavascriptInterface方法中第二參數
//它表示我們的java對象javaClass的別名.
//這樣Javascript就可以通過該別名來調用Android中的方法
//即Javascript代碼中的:window.testform.send(date+"|"+email+"|"+name+"|"+sex);
//send是方法名
//testform是別名
mWebView.addJavascriptInterface(new Object() {
public void send(String userInfo) {
StringTokenizer userInfoStringTokenizer = new StringTokenizer(userInfo, "|");
date = userInfoStringTokenizer.nextToken();
email = userInfoStringTokenizer.nextToken();
username = userInfoStringTokenizer.nextToken();
sex = userInfoStringTokenizer.nextToken();
System.out.println("userInfoStringTokenizer="+userInfoStringTokenizer.toString());
System.out.println("date=" + date);
System.out.println("email=" + email);
System.out.println("username=" + username);
System.out.println("sex=" + sex);
};
}, "testform");
}
private void initWebViewSettings(){
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.requestFocus();
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
}
}
main.xml如下:
[html]
RelativeLayout
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"
>
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
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"
>
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
form.html如下:
[javascript]
<body>
<form action="" method="post">
時間:<br>
<select id="shijian" name="date">
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select><br>
郵箱:
<input id="email" type="text" name="emailID" />
<br>
昵稱:
<input id="name" type="text" name="username" />
<br>
性別:<br>
<input id="men" type="radio" name="sex" value="men"/>男
<input id="women" type="radio" name="sex" value="women"/>女
<br>
<input type="submit" value="注冊" onclick="f()"/>
<input type="button" value="取消" />
</form>
</body>
<script type="text/JavaScript" language="javascript">
function f(){
var email = document.getElementById('email').value;
var name = document.getElementById('name').value;
var date = document.getElementById('shijian').value;
if(document.getElementById('men').checked && !document.getElementById('women').checked){
var sex = document.getElementById('men').value;
}else if(!document.getElementById('men').checked && document.getElementById('women').checked){
var sex = document.getElementById('women').value;
}
window.testform.send(date+"|"+email+"|"+name+"|"+sex);
}
</script>
<body>
<form action="" method="post">
時間:<br>
<select id="shijian" name="date">
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select><br>
郵箱:
<input id="email" type="text" name="emailID" />
<br>
昵稱:
<input id="name" type="text" name="username" />
<br>
性別:<br>
<input id="men" type="radio" name="sex" value="men"/>男
<input id="women" type="radio" name="sex" value="women"/>女
<br>
<input type="submit" value="注冊" onclick="f()"/>
<input type="button" value="取消" />
</form>
</body>
<script type="text/JavaScript" language="javascript">
function f(){
var email = document.getElementById('email').value;
var name = document.getElementById('name').value;
var date = document.getElementById('shijian').value;
if(document.getElementById('men').checked && !document.getElementById('women').checked){
var sex = document.getElementById('men').value;
}else if(!document.getElementById('men').checked && document.getElementById('women').checked){
var sex = document.getElementById('women').value;
}
window.testform.send(date+"|"+email+"|"+name+"|"+sex);
}
</script>
最近在參加CSDN博客之星,希望大家給投一票,謝謝啦~ 點這裡投我一票吧~前言 在開發當中,我們常常需要實現文件上傳,比較常
picasso是Square公司開源的一個Android圖形緩存庫,地址http://square.github.io/picasso/,可以實現圖片下載和緩存功能。
紅米手機快捷鍵使用技巧匯總。紅米手機,在市場的位置也慢慢變重要了,價格低,又實惠,又好用。那朋友們,你們知道它有那些快捷鍵的嗎?那就讓小編來跟大家詳細的介紹
1、3個相關API接口 Android網絡編程相關的API接口與相關用途如下圖 2、 2種網絡架構模式 B/S----浏覽器/服務器端模式