worklight 中添加时间控件
在我们使用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 中添加时间控件的更多相关文章
- TWaver初学实战——如何在TWaver属性表中添加日历控件?
在日期输入框中添加日历控件,是一种非常流行和实用的做法.临渊羡鱼不如退而写代码,今天就看看在TWaver中是如何实现的. 资源准备 TWaver的在线使用文档中,就有TWaver Proper ...
- 如何在VS2010中添加ActiveX控件及使用方法
方法1: 1.首先在在项目上面右击添加类,如下图所示: 2.点击添加ActiveX控件中的MFC类 3.找到需要添加的ActiveX类. 4.点击完成即可. 5.此时转到资源视图,打开如下视图.可能工 ...
- VC中添加web控件的方法
在VC中使用WebBrowser控件的两方法 黄森堂(vcmfc)著 ClassWizard方式: 1.创建包装类:View->ClassWizard->Add Class->For ...
- VS工具箱中添加DevExpress控件
关闭所有VS进程: ①使用控制台进入DevExpress安装目录: D:\DevExpress\Components\Tools\ ②添加DevExpress控件:ToolboxCreator.exe ...
- 在xib中添加手势控件后运行可能会出现的错误
如果出现错误: // -[UITapGestureRecognizer superview]: unrecognized selector sent to instance 0x8e407a0 // ...
- 在工作表左侧中添加TreeView控件
开发环境基于VSTO:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序. 需求是在sheet的左侧停靠System.Windows.Forms.TreeV ...
- 如何在vs2010中添加Picture控件
1.新建项目,并在对话框控件中拖入picture控件,并做如下设置 2.在picture控件的属性栏需要进行如下修改:ID需要修改,不能为static ID是控件的唯一标识,PictureCtrl(p ...
- Android 动态背景的实现以及SurfaceView中添加EditText控件
首先还是一贯作风,我们先看案例: \ 静态图看不出来效果,如果用过此软件(扎客)的同学们都知道,她的背景会动.怎么样,是不是觉得很时尚,起码比静态的要好(个人观点).其实实现起来并不复杂,这个如果让做 ...
- C# WinForm中添加用户控件
转:https://blog.csdn.net/haelang/article/details/40681003 有的时候我们需要频繁使用一些系统默认工具的组合,那么就可以使用自定义用户控件. 起一个 ...
随机推荐
- code first 尝试
建表: 1.先用EF连接数据库,配置connectionStrings <configSections> <!-- For more information on Entity Fr ...
- iOS开发中的Html解析方法
iOS开发中的Html解析方法 本文作者为大家介绍了在iOS开发中的Html解析方法,并同时提供了Demo代码的下载链接,Demo 解析了某个网站(具体可在代码中查看)的html网页,提取了图片以及标 ...
- CM源码同步编译教程
一.操作系统 准备一个ubuntu安装镜像,我用的是12.10. 安装系统时请安装到30g以上的盘,最好就50g以上啦 安装教程不多说,网上很多教程,这里我推荐用自带的wubi工具安装,比较方便 用虚 ...
- 学习ASP.NET的一些学习资源
ASP.NET学习相关资源 当我们在决定选择哪一个编程语言来做web开发的时候,很难选择,php.java.python这些语言是开源的,有很多的学习资源,但是当我们决定学习ASP.NET的时候,微软 ...
- 漫游Ruby
Ruby是一门完全面向对象的编程语言,Ruby中的每个值都是对象(nil是Ruby总的特殊值代表null),以下是在irb中的案例. 在Ruby中,圆括号通常都是可选的而且一般都被省略掉. Ruby中 ...
- Teach Yourself Scheme in Fixnum Days 13 Jump跳转
Jumps One of the signal features of Scheme is its support for jumps or nonlocal control. Specificall ...
- VC++如何在程序中用代码注册和卸载ocx控件(代码)
方法一:在dos或Windows命令行下运行:regsvr32 ocxname.ocx 注册 示例:regsvr32 netshare.ocx //注册netshare.ocx控件regsvr ...
- poj2752 Seek the Name, Seek the Fame
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...
- bzoj1047-理想的正方形(二维单调队列)
题意: 给一个矩阵,给出行列和每个数,再给出一个N,求出所有N*N的子矩阵中最大值最小值之差的最小值解析: 暴力枚举肯定不行,这题可以用二维单调队列做,把同一行的连续N个点缩成一个点保存最大最小值预处 ...
- poj 2441 Arrange the Bulls(状态压缩dp)
Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to ...