博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Phonegap中自定义插件的使用(日期选择器)
阅读量:6036 次
发布时间:2019-06-20

本文共 5068 字,大约阅读时间需要 16 分钟。

hot3.png

注:文章转自:

DatePickerPlugin.java:

package org.apache.cordova;import java.util.Calendar;import org.apache.cordova.api.Plugin;import org.apache.cordova.api.PluginResult;import org.apache.cordova.api.PluginResult.Status;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.app.DatePickerDialog;import android.app.DatePickerDialog.OnDateSetListener;import android.app.TimePickerDialog;import android.app.TimePickerDialog.OnTimeSetListener;import android.content.Context;import android.util.Log;import android.widget.DatePicker;import android.widget.TimePicker;public class DatePickerPlugin extends Plugin {	private static final String ACTION_DATE = "date";	private static final String ACTION_TIME = "time";	@Override	public PluginResult execute(final String action, final JSONArray data,			final String callBackId) {		Log.d("DatePickerPlugin", "Plugin Called");		PluginResult result = null;		if (ACTION_DATE.equalsIgnoreCase(action)) {			Log.d("DatePickerPluginListener execute", ACTION_DATE);			this.showDatePicker(callBackId);			final PluginResult r = new PluginResult(					PluginResult.Status.NO_RESULT);			r.setKeepCallback(true);			return r;		} else if (ACTION_TIME.equalsIgnoreCase(action)) {			Log.d("DatePickerPluginListener execute", ACTION_TIME);			this.showTimePicker(callBackId);			final PluginResult r = new PluginResult(					PluginResult.Status.NO_RESULT);			r.setKeepCallback(true);			return r;		} else {			result = new PluginResult(Status.INVALID_ACTION);			Log.d("DatePickerPlugin", "Invalid action : " + action + " passed");		}		return result;	}	public synchronized void showTimePicker(final String callBackId) {		final DatePickerPlugin datePickerPlugin = this;		final Context currentCtx = cordova.getActivity();		final Runnable runnable = new Runnable() {			public void run() {				final TimePickerDialog tpd = new TimePickerDialog(currentCtx,						new OnTimeSetListener() {							public void onTimeSet(final TimePicker view,									final int hourOfDay, final int minute) {								final JSONObject userChoice = new JSONObject();								try {									userChoice.put("hour", hourOfDay);									userChoice.put("min", minute);								} catch (final JSONException jsonEx) {									Log.e("showDatePicker",											"Got JSON Exception "													+ jsonEx.getMessage());									datePickerPlugin.error(new PluginResult(											Status.JSON_EXCEPTION), callBackId);								}								datePickerPlugin.success(new PluginResult(										PluginResult.Status.OK, userChoice),										callBackId);							}						}, 1, 1, true);				tpd.show();			}		};		ctx.runOnUiThread(runnable);	}	public synchronized void showDatePicker(final String callBackId) {		final DatePickerPlugin datePickerPlugin = this;		final Context currentCtx = cordova.getActivity();		final Calendar c = Calendar.getInstance();		final int mYear = c.get(Calendar.YEAR);		final int mMonth = c.get(Calendar.MONTH);		final int mDay = c.get(Calendar.DAY_OF_MONTH);		final Runnable runnable = new Runnable() {			public void run() {				final DatePickerDialog dpd = new DatePickerDialog(currentCtx,						new OnDateSetListener() {							public void onDateSet(final DatePicker view,									final int year, final int monthOfYear,									final int dayOfMonth) {								final JSONObject userChoice = new JSONObject();								try {									userChoice.put("year", year);									userChoice.put("month", monthOfYear);									userChoice.put("day", dayOfMonth);								} catch (final JSONException jsonEx) {									Log.e("showDatePicker",											"Got JSON Exception "													+ jsonEx.getMessage());									datePickerPlugin.error(new PluginResult(											Status.JSON_EXCEPTION), callBackId);								}								datePickerPlugin.success(new PluginResult(										PluginResult.Status.OK, userChoice),										callBackId);							}						}, mYear, mMonth, mDay);				dpd.show();			}		};		ctx.runOnUiThread(runnable);	}}

在config.xml添加:

phonegap-datePicker.js:

/** * 日期插件 *  * @author cjianquan * @since 2014-07-16 *  */var DatePicker = function() {};DatePicker.prototype.showDateOrTime = function(action, successCallback,		failureCallback) {	return PhoneGap.exec(successCallback, // Success callback from the plugin	failureCallback, // Error callback from the plugin	'DatePickerPlugin', // Tell PhoneGap to run "DatePickerPlugin" Plugin	action, // Tell plugin, which action we want to perform	[]); // Passing list of args to the plugin};/** * Enregistre une nouvelle bibliothèque de fonctions auprès de PhoneGap */PhoneGap.addConstructor(function() {	// 如果不支持window.plugins,则创建并设置	if (!window.plugins) {		window.plugins = {};	}	window.plugins.datePickerPlugin = new DatePicker();	// 向phonegap中注入插件相关的js	// Register the javascript plugin with PhoneGap	PhoneGap.addPlugin('datePickerPlugin', new DatePicker());	// phonegap中注入后台插件相关的java类	// Register the native class of plugin with PhoneGap	PluginManager.addService("DatePickerPlugin",			"org.apache.cordova.DatePickerPlugin");});
在html页面中:

转载于:https://my.oschina.net/u/2552902/blog/543902

你可能感兴趣的文章
tomcat如何修改发布目录
查看>>
CentOS 5.5 使用 EPEL 和 RPMForge 软件库
查看>>
Damien Katz弃Apache CouchDB,继以Couchbase Server
查看>>
Target runtime Apache Tomcat is not defined.错误解决方法
查看>>
某机字长为32位,存储容量为64MB,若按字节编址.它的寻址范围是多少?
查看>>
VC++ 监视文件(夹)
查看>>
【转】keyCode对照表及JS监听组合按键
查看>>
[Java开发之路](14)反射机制
查看>>
mac gentoo-prefix安装git svn
查看>>
浅尝异步IO
查看>>
C - Train Problem II——(HDU 1023 Catalan 数)
查看>>
Speak loudly
查看>>
iOS-在项目中引入RSA算法
查看>>
[译] 听说你想学 React.js ?
查看>>
gulp压缩合并js与css
查看>>
块级、内联、内联块级
查看>>
Predicate
查看>>
[面试题记录01]实现一个function sum达到一下目的
查看>>
这个季节的忧伤,点到为止
查看>>
mysql通过配置文件进行优化
查看>>