worklight 中添加时间控件

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

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

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

实现日期插件

  1 public class DatePickerPlugin extends CordovaPlugin {
  2
  3         private static final CordovaActivity ctx = null;
  4         private static final String ACTION_DATE = "date";
  5         private static final String ACTION_TIME = "time";
  6
  7         @Override
  8         public boolean execute(String action, JSONArray args,
  9                 CallbackContext callbackContext) throws JSONException {
 10             Log.d("DatePickerPlugin", "Plugin Called");
 11             PluginResult result = null;
 12             if (ACTION_DATE.equalsIgnoreCase(action)) {
 13                 Log.d("DatePickerPluginListener execute", ACTION_DATE);
 14                 this.showDatePicker(callbackContext.getCallbackId(),callbackContext);
 15                 result= new PluginResult(
 16                         PluginResult.Status.NO_RESULT);
 17                 result.setKeepCallback(true);
 18             } else if (ACTION_TIME.equalsIgnoreCase(action)) {
 19                 Log.d("DatePickerPluginListener execute", ACTION_TIME);
 20                 this.showTimePicker(callbackContext.getCallbackId(),callbackContext);
 21                 result = new PluginResult(
 22                         PluginResult.Status.NO_RESULT);
 23                 result.setKeepCallback(true);
 24             } else {
 25                 result = new PluginResult(Status.INVALID_ACTION);
 26                 Log.d("DatePickerPlugin", "Invalid action : " + action + " passed");
 27             }
 28             return false;
 29         }
 30         /**
 31          * 时分实现
 32          * @param callBackId
 33          * @param callbackContext
 34          */
 35         public synchronized void showTimePicker(final String callBackId,final CallbackContext callbackContext) {
 36             final Calendar c = Calendar.getInstance();
 37             final int mWhen = c.get(Calendar.HOUR_OF_DAY);
 38             final int mMin = c.get(Calendar.MINUTE);
 39
 40             final Runnable runnable = new Runnable() {
 41                 public void run() {
 42                     final TimePickerDialog tpd = new TimePickerDialog(cordova.getActivity(),
 43                             new OnTimeSetListener() {
 44
 45                                 public void onTimeSet(final TimePicker view,
 46                                         final int hourOfDay, final int minute) {
 47                                     final JSONObject userChoice = new JSONObject();
 48                                     try {
 49                                         userChoice.put("hour", hourOfDay);
 50                                         userChoice.put("min", minute);
 51                                     } catch (final JSONException jsonEx) {
 52                                         Log.e("showDatePicker",
 53                                                 "Got JSON Exception "
 54                                                         + jsonEx.getMessage());
 55                                         callbackContext.sendPluginResult(new PluginResult(
 56                                                 Status.JSON_EXCEPTION));
 57                                     }
 58                                     callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,
 59                                             userChoice));
 60                                 }
 61                             }, mWhen, mMin, true);
 62
 63                     tpd.show();
 64                 }
 65             };
 66             this.cordova.getActivity().runOnUiThread(runnable);
 67         }
 68         /**
 69          * 年月日实现
 70          * @param callBackId
 71          * @param callbackContext
 72          */
 73         public synchronized void showDatePicker(final String callBackId,final CallbackContext callbackContext) {
 74
 75             final Calendar c = Calendar.getInstance();
 76             final int mYear = c.get(Calendar.YEAR);
 77             final int mMonth = c.get(Calendar.MONTH);
 78             final int mDay = c.get(Calendar.DAY_OF_MONTH);
 79
 80             final Runnable runnable = new Runnable() {
 81
 82                 public void run() {
 83                     final DatePickerDialog dpd = new DatePickerDialog(cordova.getActivity(),
 84                             new OnDateSetListener() {
 85
 86                                 public void onDateSet(final DatePicker view,
 87                                         final int year, final int monthOfYear,
 88                                         final int dayOfMonth) {
 89
 90                                     final JSONObject userChoice = new JSONObject();
 91
 92                                     try {
 93                                         userChoice.put("year", year);
 94                                         userChoice.put("month", monthOfYear);
 95                                         userChoice.put("day", dayOfMonth);
 96                                     } catch (final JSONException jsonEx) {
 97                                         Log.e("showDatePicker",
 98                                                 "Got JSON Exception "
 99                                                         + jsonEx.getMessage());
100                                         callbackContext.sendPluginResult(new PluginResult(
101                                                  Status.JSON_EXCEPTION, userChoice));
102                                     }
103                                     callbackContext.sendPluginResult(new PluginResult(
104                                             PluginResult.Status.OK, userChoice));
105
106                                 }
107                             }, mYear, mMonth, mDay);
108
109                     dpd.show();
110                 }
111             };
112             this.cordova.getActivity().runOnUiThread(runnable);
113         }
114     }  

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

 1 var DatePicker = function() {
 2 };
 3 DatePicker.prototype.showDateOrTime = function(action,successCallback, failureCallback) {
 4     cordova.exec(
 5         successCallback,
 6         failureCallback,
 7         ‘DatePickerPlugin‘,  //Tell PhoneGap to run "DatePickerPlugin" Plugin
 8         action,              //Tell plugin, which action we want to perform
 9         []        //paramter
10     );        //Passing list of args to the plugin
11 };
12 cordova.addConstructor(function(){
13     //如果不支持window.plugins,则创建并设置
14     if(!window.plugins){
15         window.plugins={};
16     }
17     window.plugins.datePickerPlugin=new DatePicker();
18     cordova.addPlugin(‘datePickerPlugin‘, new DatePicker());
19     PluginManager.addService("DatePickerPlugin",
20     "cn.gcsts.plugin.DatePickerPlugin");
21 });

效果图展示

worklight 中添加时间控件

时间: 2024-08-03 06:54:37

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

JScrollPane控件中添加其他控件的问题&&JScrollPane设置滚动条&&调整滚动速度

如果要在JScrollPane控件中添加其他控件,不能用下面这种方法 JScrollPane j = new JScrollPane();j.add(new JButton("点击")); 只能通过以下方法添加 JScrollPane j = new JScrollPane(new JButton("点击")); 或者 JScrollPane j = new JScrollPane();j.getViewport().add(new JButton("点击

IOS之导航栏中添加UITextView控件bug

今天遇到一个奇怪的问题,如下: 在导航栏控制器的rootviewcontroller中,添加了一个UITextView控件,代码如下: - (void)viewDidLoad { [super viewDidLoad]; self.title =@"Test"; UITextView *textview = [[UITextViewalloc]init]; textview.frame = CGRectMake(10, 100, 300, 200); textview.backgrou

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

      首先还是一贯作风,我们先看案例: 静态图看不出来效果,如果用过此软件(扎客)的同学们都知道,她的背景会动.怎么样,是不是觉得很时尚,起码比静态的要好(个人观点).其实实现起来并 不复杂,这个如果让做游戏程序员做简直太小儿科了,这里我说明一点,其实我们做应用的也应该多少了解下游戏编程思维,起码对我们做应用有很好的帮助. 下面我简单介绍下实现方式. 实现原理:自定义一个SurfaceView控件.对之不停的onDraw,使得其背景动起来. 对于SurfaceView如果不了解的同学们麻烦

VC中添加web控件的方法

在VC中使用WebBrowser控件的两方法 黄森堂(vcmfc)著 ClassWizard方式: 1.创建包装类:View->ClassWizard->Add Class->Form a Type Library->C:\winnt\system32\shdocvw.dll->只选择IWebBrowserApp类->OK->OK 2.声明一个类变量:IWebBrowserApp m_internetexplorer;,并包含刚才的头文件(xxx.h) 3.在类的

005.使用百度SDK写hello baidumap时,在布局xml文件中添加地图控件时;提示'clickable' attribute found, please also add 'focusable' 错误

0.报错&提示信息: 'clickable' attribute found, please also add 'focusable'  A widget that is declared to be clickable but not declared to be focusable is not accessible via the keyboard. Please add the focusable attribute as well. 1.原因: 一个控件,如果没有定义focusable

VS工具箱中添加DevExpress控件

关闭所有VS进程: ①使用控制台进入DevExpress安装目录: D:\DevExpress\Components\Tools\ ②添加DevExpress控件:ToolboxCreator.exe/ini:toolboxcreator.ini ③移除DevExpress控件:ToolboxCreator.exe/ini:toolboxcreator.ini/remove 注:执行至过程②时打开VS就会自动加载.

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

开发环境基于VSTO:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序. 需求是在sheet的左侧停靠System.Windows.Forms.TreeView控件,实现类似资源浏览器的效果,另外,tree节点使用自定义的图标,支持复选框. 首先准备好树节点的图标,使用visual studio 2010自带的图标可以省去很多麻烦(在安装目录\Common7\VS2010ImageLibrary).我挑选了4个16x16大小的图标拷贝到vsto工程下

Fragment中添加spinner控件问题

今天编写Android程序,遇到一个问题: fragment是activity的一部分,具有高度的自由性.我编写了一个Fragment程序,在其中添加了Spinner控件(就是普通的添加方式),但是就是运用ArrayAdapter进行数据绑定的时候 ArrayAdapter<String>(View,int,String)中的View类型参数,并不能用常用的this来代替.必须用getActivity().getBaseContext()来代替. 下面来说明一下具体的原因: this应用上下文

解决UIScrollView中添加子控件出现“UIScrollView Scrollable Content Size Ambiguity”的办法

来自StackOverflow的帖子,原文见这里. So I just sorted out in this way: Add in the UIScrollView a UIView (we can call that contentView); In this contentView, set top, bottom, left and right margins to 0 (of course from the scrollView which is the superView); Set