Android本身就有DatePicker和TimePicker控件的代码如下
1import java.util.Calendar;
2import org.json.JSONException;
3import org.json.JSONObject;
4import android.app.Activity;
5import android.app.DatePickerDialog;
6import android.app.DatePickerDialog.OnDateSetListener;
7import android.os.Bundle;
8import android.os.Handler;
9import android.webkit.WebView;
10import android.widget.DatePicker;
1112publicclass MyWebViewTestActivity extends Activity {
13public WebView webView;
1415// Handler handler;1617 @Override
18publicvoid onCreate(Bundle savedInstanceState) {
19super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
2122 webView = (WebView) findViewById(R.id.myWebView);
28 webView.getSettings().setJavaScriptEnabled(true);
29 webView.addJavascriptInterface(new ProxyBridge(), "ProxyBridge");
30 webView.loadUrl("file:///android_asset/index.html");
31 }
3233class ProxyBridge {
34 String date;
35 String json_date;
36 DatePickerDialog pickerDialog;
37 Handler handler = new Handler();
38publicvoid getDateTime() {
39 handler.post(new Runnable() {
40 @Override
41publicvoid run() {
42final Calendar cd = Calendar.getInstance();
43 pickerDialog = new DatePickerDialog(
44 MyWebViewTestActivity.this,
45new OnDateSetListener() {
4647 @Override
48publicvoid onDateSet(DatePicker view,
49int year, int monthOfYear,
50int dayOfMonth) {
51try {
52 date = String.valueOf(year) + "-"
53 + String.valueOf(monthOfYear)
54 + "-"
55 + String.valueOf(dayOfMonth);
56 JSONObject map = new JSONObject();
57 map.put("date", date);
58// json_date = "[{\"time\":\"2012-02-18\"}]";59 json_date = map.toString();
60 } catch (JSONException e) {
61 e.printStackTrace();
62 }
63 webView.loadUrl("javascript:setDateTime("
64 + json_date + ")");
65 }
66 }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH),
67 cd.get(Calendar.DAY_OF_MONTH));
68 pickerDialog.show();
69 }
70 });
71 };
73 }
74 }
以下是HTML的测试代码
<html>
<head>
<title>日期选择测试</title>
<script language=
"javascript"
>
function
setDateTime(JSONdate){
var
jsonobjs = eval(JSONdate);
var
date=jsonobjs.date;
document.getElementById(
"datetext"
).value=date;
}
</script>
</head>
<body>
<form>
<input type=
"text"
id=
"datetext"
onclick=
"window.ProxyBridge.getDateTime();"
value=
""
/>
</form>
</body>
</html>
ss
2import org.json.JSONException;
3import org.json.JSONObject;
4import android.app.Activity;
5import android.app.DatePickerDialog;
6import android.app.DatePickerDialog.OnDateSetListener;
7import android.os.Bundle;
8import android.os.Handler;
9import android.webkit.WebView;
10import android.widget.DatePicker;
1112publicclass MyWebViewTestActivity extends Activity {
13public WebView webView;
1415// Handler handler;1617 @Override
18publicvoid onCreate(Bundle savedInstanceState) {
19super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
2122 webView = (WebView) findViewById(R.id.myWebView);
28 webView.getSettings().setJavaScriptEnabled(true);
29 webView.addJavascriptInterface(new ProxyBridge(), "ProxyBridge");
30 webView.loadUrl("file:///android_asset/index.html");
31 }
3233class ProxyBridge {
34 String date;
35 String json_date;
36 DatePickerDialog pickerDialog;
37 Handler handler = new Handler();
38publicvoid getDateTime() {
39 handler.post(new Runnable() {
40 @Override
41publicvoid run() {
42final Calendar cd = Calendar.getInstance();
43 pickerDialog = new DatePickerDialog(
44 MyWebViewTestActivity.this,
45new OnDateSetListener() {
4647 @Override
48publicvoid onDateSet(DatePicker view,
49int year, int monthOfYear,
50int dayOfMonth) {
51try {
52 date = String.valueOf(year) + "-"
53 + String.valueOf(monthOfYear)
54 + "-"
55 + String.valueOf(dayOfMonth);
56 JSONObject map = new JSONObject();
57 map.put("date", date);
58// json_date = "[{\"time\":\"2012-02-18\"}]";59 json_date = map.toString();
60 } catch (JSONException e) {
61 e.printStackTrace();
62 }
63 webView.loadUrl("javascript:setDateTime("
64 + json_date + ")");
65 }
66 }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH),
67 cd.get(Calendar.DAY_OF_MONTH));
68 pickerDialog.show();
69 }
70 });
71 };
73 }
74 }
<html>
<head>
<title>日期选择测试</title>
<script language=
"javascript"
>
function
setDateTime(JSONdate){
var
jsonobjs = eval(JSONdate);
var
date=jsonobjs.date;
document.getElementById(
"datetext"
).value=date;
}
</script>
</head>
<body>
<form>
<input type=
"text"
id=
"datetext"
onclick=
"window.ProxyBridge.getDateTime();"
value=
""
/>
</form>
</body>
</html>