转:Delplhi 实现Java Jar包中的Listener
Java中, 经常会有setOnXXXListener的方法. 但是, 在Delphi中的调用, 就不是那么方便了, 现记录方法为如下.
以下面的Java调用为例: 在Delphi中实现MapView控件的OnStatusChangedListener监听:
mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
private static final long serialVersionUID = 1L;
@Override
public void onStatusChanged(Object arg0, STATUS arg1) {
if (arg0 == mMapView && arg1 == STATUS.INITIALIZED) {
// 设置初始的缩放
mMapView.setScale(C_DEFAULT_SCALE);
}
}
});
Delphi中的步骤如下:
1. 将Jar包生成Pas文件, 摘取相关代码如下:
[JavaSignature('com/esri/android/map/MapView')]
JMapView = interface(JViewGroup) // or JObject // SuperSignature: android/view/ViewGroup
['{E5D559D4-41EE-4577-8F6F-EEABC73FF2B9}']
{ Property Methods }
{ methods }
......
function getOnStatusChangedListener: JOnStatusChangedListener; cdecl; //()Lcom/esri/android/map/event/OnStatusChangedListener;
procedure setOnStatusChangedListener(onStatusChangedListener: JOnStatusChangedListener); cdecl; //(Lcom/esri/android/map/event/OnStatusChangedListener;)V
......
{ Property }
end;
JOnStatusChangedListenerClass = interface(JObjectClass)
['{48C131AA-6C7F-4A06-B65F-4A06316A5437}']
{ static Property Methods }
{ static Methods }
{ static Property }
end;
[JavaSignature('com/esri/android/map/event/OnStatusChangedListener')]
JOnStatusChangedListener = interface(JObject)
['{287754FD-EC7A-4A7C-BA37-34DA2D3DAAF6}']
{ Property Methods }
{ methods }
procedure onStatusChanged(P1: JObject; P2: JOnStatusChangedListener_STATUS); cdecl; //(Ljava/lang/Object;Lcom/esri/android/map/event/OnStatusChangedListener$STATUS;)V
{ Property }
end;
TJOnStatusChangedListener = class(TJavaGenericImport<JOnStatusChangedListenerClass, JOnStatusChangedListener>) end;
2. 创建新的类, 实现JOnStatusChangedListener:
TOnStatusChangeListener = class(TJavaLocal, JOnStatusChangedListener)
private
FParent : TFrameMain;
public
constructor Create(parent: TFrameMain);
function equals(o: JObject): boolean; cdecl;
function getClass: Jlang_Class; cdecl;
function hashCode: integer; cdecl;
procedure notify; cdecl;
procedure notifyAll; cdecl;
function toString: JString; cdecl;
procedure wait; overload; cdecl;
procedure wait(millis: Int64); overload; cdecl;
procedure wait(millis: Int64; nanos: integer); overload; cdecl;
procedure onStatusChanged(P1: JObject; P2: JOnStatusChangedListener_STATUS); cdecl;
end;
3. 实现方法:
constructor TOnStatusChangeListener.Create(parent: TFrameMain);
begin
inherited Create; // Create的时候, 不要忘记这一句, 否则Listener不起作用
Self.FParent := parent;
end;
procedure TOnStatusChangeListener.onStatusChanged(P1: JObject;
P2: JOnStatusChangedListener_STATUS);
begin
// 0 - 地图加载完毕
if P2.getValue = 0 then
begin
Self.FParent.OnFinishedMap();
end;
end;
function TOnStatusChangeListener.equals(o: JObject): boolean;
begin
end;
function TOnStatusChangeListener.getClass: Jlang_Class;
begin
end;
function TOnStatusChangeListener.hashCode: integer;
begin
end;
procedure TOnStatusChangeListener.notify;
begin
end;
procedure TOnStatusChangeListener.notifyAll;
begin
end;
function TOnStatusChangeListener.toString: JString;
begin
end;
procedure TOnStatusChangeListener.wait;
begin
end;
procedure TOnStatusChangeListener.wait(millis: Int64);
begin
end;
procedure TOnStatusChangeListener.wait(millis: Int64; nanos: integer);
begin
end;
————————————————
版权声明:本文为CSDN博主「ssxbxk」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ssxbxk/java/article/details/71642119