在我们使用worklight开发的过程中,由于文档的不开源和插件的缺少,总是自己琢磨很多东东,更有胜者

需要调用源代码实现某些不易实现的功能。在这里把实现的功能代码贴出来,如有不足之处还望指正!

实现的步骤就不多说了,上篇中已经解说

实现日期插件

 public class DatePickerPlugin extends CordovaPlugin {

         private static final CordovaActivity ctx = null;
private static final String ACTION_DATE = "date";
private static final String ACTION_TIME = "time"; @Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
Log.d("DatePickerPlugin", "Plugin Called");
PluginResult result = null;
if (ACTION_DATE.equalsIgnoreCase(action)) {
Log.d("DatePickerPluginListener execute", ACTION_DATE);
this.showDatePicker(callbackContext.getCallbackId(),callbackContext);
result= new PluginResult(
PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
} else if (ACTION_TIME.equalsIgnoreCase(action)) {
Log.d("DatePickerPluginListener execute", ACTION_TIME);
this.showTimePicker(callbackContext.getCallbackId(),callbackContext);
result = new PluginResult(
PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
} else {
result = new PluginResult(Status.INVALID_ACTION);
Log.d("DatePickerPlugin", "Invalid action : " + action + " passed");
}
return false;
}
/**
* 时分实现
* @param callBackId
* @param callbackContext
*/
public synchronized void showTimePicker(final String callBackId,final CallbackContext callbackContext) {
final Calendar c = Calendar.getInstance();
final int mWhen = c.get(Calendar.HOUR_OF_DAY);
final int mMin = c.get(Calendar.MINUTE); final Runnable runnable = new Runnable() {
public void run() {
final TimePickerDialog tpd = new TimePickerDialog(cordova.getActivity(),
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());
callbackContext.sendPluginResult(new PluginResult(
Status.JSON_EXCEPTION));
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,
userChoice));
}
}, mWhen, mMin, true); tpd.show();
}
};
this.cordova.getActivity().runOnUiThread(runnable);
}
/**
* 年月日实现
* @param callBackId
* @param callbackContext
*/
public synchronized void showDatePicker(final String callBackId,final CallbackContext callbackContext) { 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(cordova.getActivity(),
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());
callbackContext.sendPluginResult(new PluginResult(
Status.JSON_EXCEPTION, userChoice));
}
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.OK, userChoice)); }
}, mYear, mMonth, mDay); dpd.show();
}
};
this.cordova.getActivity().runOnUiThread(runnable);
}
}

当然的必须将实现类配置在config.xml中

 var DatePicker = function() {
};
DatePicker.prototype.showDateOrTime = function(action,successCallback, failureCallback) {
cordova.exec(
successCallback,
failureCallback,
'DatePickerPlugin', //Tell PhoneGap to run "DatePickerPlugin" Plugin
action, //Tell plugin, which action we want to perform
[] //paramter
); //Passing list of args to the plugin
};
cordova.addConstructor(function(){
//如果不支持window.plugins,则创建并设置
if(!window.plugins){
window.plugins={};
}
window.plugins.datePickerPlugin=new DatePicker();
cordova.addPlugin('datePickerPlugin', new DatePicker());
PluginManager.addService("DatePickerPlugin",
"cn.gcsts.plugin.DatePickerPlugin");
});

效果图展示

worklight 中添加时间控件的更多相关文章

  1. TWaver初学实战——如何在TWaver属性表中添加日历控件?

    在日期输入框中添加日历控件,是一种非常流行和实用的做法.临渊羡鱼不如退而写代码,今天就看看在TWaver中是如何实现的.   资源准备   TWaver的在线使用文档中,就有TWaver Proper ...

  2. 如何在VS2010中添加ActiveX控件及使用方法

    方法1: 1.首先在在项目上面右击添加类,如下图所示: 2.点击添加ActiveX控件中的MFC类 3.找到需要添加的ActiveX类. 4.点击完成即可. 5.此时转到资源视图,打开如下视图.可能工 ...

  3. VC中添加web控件的方法

    在VC中使用WebBrowser控件的两方法 黄森堂(vcmfc)著 ClassWizard方式: 1.创建包装类:View->ClassWizard->Add Class->For ...

  4. VS工具箱中添加DevExpress控件

    关闭所有VS进程: ①使用控制台进入DevExpress安装目录: D:\DevExpress\Components\Tools\ ②添加DevExpress控件:ToolboxCreator.exe ...

  5. 在xib中添加手势控件后运行可能会出现的错误

    如果出现错误: // -[UITapGestureRecognizer superview]: unrecognized selector sent to instance 0x8e407a0 // ...

  6. 在工作表左侧中添加TreeView控件

    开发环境基于VSTO:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序. 需求是在sheet的左侧停靠System.Windows.Forms.TreeV ...

  7. 如何在vs2010中添加Picture控件

    1.新建项目,并在对话框控件中拖入picture控件,并做如下设置 2.在picture控件的属性栏需要进行如下修改:ID需要修改,不能为static ID是控件的唯一标识,PictureCtrl(p ...

  8. Android 动态背景的实现以及SurfaceView中添加EditText控件

    首先还是一贯作风,我们先看案例: \ 静态图看不出来效果,如果用过此软件(扎客)的同学们都知道,她的背景会动.怎么样,是不是觉得很时尚,起码比静态的要好(个人观点).其实实现起来并不复杂,这个如果让做 ...

  9. C# WinForm中添加用户控件

    转:https://blog.csdn.net/haelang/article/details/40681003 有的时候我们需要频繁使用一些系统默认工具的组合,那么就可以使用自定义用户控件. 起一个 ...

随机推荐

  1. U盘装系统系列三—-ghost系统安装教程

    前面和大家分享了如何用老毛桃U盘启动盘制作工具把U盘制作启动盘,接下来说下制作好启动盘之后如何安装ghost系统.首先我们准备好ghost镜像复制到U盘中:然后用U盘启动:选择[01]后按Enter键 ...

  2. IOC容器MEF在MVC中的使用

    最近想把自己的网站框架用IOC改造下,经过对比,我初步选择autofac,虽然MEF不需要配置,但性能不行,autofac虽然需要自己写自动化注入,但性能非常好. 先分析下各大IOC框架的性能,分两类 ...

  3. iOS开发 masonry 设置tableHeadView

    最近做公司项目,使用到到tableHeadView,一直习惯用masonry来设置约束,但是设置tableHeadView没有那么的简单.先看下效果图: 视图层次结构是这样的: 基础的创建工程项目之类 ...

  4. iOS 容易引“起循环引用”的三种场景

    笔者在阅读中总结了一下,在iOS平台容易引起循环引用的四个场景: 一.parent-child相互持有.委托模式 [案例]:   @interface FTAppCenterMainViewContr ...

  5. Effective Java Item3:Enforce the singleton property with a private constructor or an enum type

    Item3:Enforce the singleton property with a private constructor or an enum type 采用枚举类型(ENUM)实现单例模式. ...

  6. Android数据存储

    SharedPreferences----------->for primitive data Internal Storage-------------->for private dat ...

  7. Altium Designer (protel) 各版本“故障”随谈

    Altium 的版本很多,每个版本都或多或少有些可容忍或可不容忍的问题,此贴只是希望各位能将遇到的问题写出来,只是希望 给还在使用 altium 的网友一些参考,也希望有些能被 altium 所接受@ ...

  8. C++11 in Qt5

    本文转载自:http://woboq.com/blog/cpp11-in-qt5.html   C++11 in Qt5 Posted by Olivier Goffart on 11 June 20 ...

  9. BZOJ1689: [Usaco2005 Open] Muddy roads

    1689: [Usaco2005 Open] Muddy roads Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 147  Solved: 107[Su ...

  10. Android ExpandableListActivity的简单介绍及小例子

    Android中常常要用到ListView,但也经常要用到ExpandableListView,ListView是显示列表,而ExpandableListView显示的是分类的列表: 下面用一个例子来 ...