titanium module java基础: 从js到java的类型转换。

访问量: 610

refer to:  http://docs.appcelerator.com/titanium/3.0/#!/guide/Android_Module_Architecture

Titanium module中,势必要在js代码中调用java代码,也就是说把js代码作为参数传递到java代码中。这就涉及到了类型转换。

转换表如下: (第三列是在java 代码中显示调用方法)

JavaScript Type

Java Type

Explicit Conversion

Number

int

TiConvert.toInt

Number

float

TiConvert.toFloat

Number

double

TiConvert.toDouble

boolean

boolean

TiConvert.toBoolean

String

String

TiConvert.toString or Cast

Object

HashMap<String, Object>

Cast

Array

Object[]

Cast

Date

Date

TiConvert.toDate or Cast

Ti.Blob

TiBlob

TiConvert.toBlob or Cast

Ti.Filesystem.File

FileProxy

Cast

一个例子:

@Kroll.method
public int multiply(int a, int b)
{ 
 return a * b;
}

等同于:

@Kroll.method
public int multiply(Object a, Object b){
  temp_a = TiConvert.toInt(a); 
  temp_b = TiConvert.toInt(b);
  return temp_a * temp_b ;
}

订阅/RSS Feed

Subscribe

分类/category