PDA

View Full Version : استفاده از Android Native Controls در بستر FireMonkey



yellowbell
شنبه 24 مهر 1395, 15:57 عصر
با عرض ادب و احترام خدمت تمامی دوستداران دلفی

- یک سوال از اساتید گرانقدر داشتم . به طور مثال ایا امکان ایجاد یک JTextView بروی یک Form طراحی شده با Firemonkey وجود دارد ؟

بنده کلی اینترنت رو طی 2 ماه زیر و رو کردم چیزی پیدا نکرده ام . البته هدفم هم دور زدن محدودیت است که با زبان های راست چین مثل فارسی وجود دارد .

همون طور که مستحضر می باشید ایجاد یا تغییر یک شی بصری در Firemonkey بایستی در Thread اصلی برنامه که همان FMXNativeActivity است فراخوانی شود . با استفاده از توابع CallInUIThread یا CallInUIThreadAndWaitFinishing .

یک مثال ساده :


var
AJButton : JButton;
JPPP : JViewGroup_LayoutParams;

begin
CallInUIThread(
procedure ()
begin
AJButton := TJButton.JavaClass.init(TAndroidHelper.Context);
AJButton.setX(10);
AJButton.setY(10);
AJButton.setText(TJCharSequence.Wrap( StringToJNIString( TJNIResolver.GetJNIEnv, 'Masoud' ) ));
JPPP := TJViewGroup_LayoutParams.Wrap(TJViewGroup_LayoutPa rams.JavaClass.init(TJViewGroup_LayoutParams.JavaC lass.WRAP_CONTENT,TJViewGroup_LayoutParams.JavaCla ss.WRAP_CONTENT));
SharedActivity.addContentView(AJButton,JPPP);
end);




با اجرای این کد در فرم چیزی رویت نمی شود ؟!؟!

پیشاپیش از وقتی که می گذارید تشکر می کنم

nice boy
چهارشنبه 07 تیر 1396, 08:14 صبح
شما احتیاج به یک TJNativeLayout و ContentRect دارید تا بتونید Object مورد نظر رو نمایش بدید.
در کد زیر این کار رو انجام دادم



interface


uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, Androidapi.JNI.GraphicsContentViewText
,Androidapi.Helpers, Androidapi.JNI.Embarcadero, Androidapi.JNI.Widget, FMX.Platform.Android,
FMX.Controls.Presentation, FMX.Platform;


type
THeaderFooterForm = class(TForm)
Header: TToolBar;
Footer: TToolBar;
HeaderLabel: TLabel;
Button1: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FRealBounds: TRect;
FBounds: TRect;
FJNativeLayout: JNativeLayout;
FJEdit: JEditText;
FScale: Single;
procedure CalcRealBorder;
procedure InitUIThread;
procedure UpdateContentFromControl;
procedure ShowUI;
procedure HideUI;
public
{ Public declarations }
end;


var
HeaderFooterForm: THeaderFooterForm;


implementation


{$R *.fmx}


procedure THeaderFooterForm.Button1Click(Sender: TObject);
//var
// ScreenSrv: IFMXScreenService;
begin
CalcRealBorder;
FScale := 1;
InitUIThread;
UpdateContentFromControl;
end;


procedure THeaderFooterForm.CalcRealBorder;
var
NativeWin: JWindow;
ContentRect: JRect;
begin
NativeWin := TAndroidHelper.Activity.getWindow;
if NativeWin <> nil then
begin
ContentRect := TJRect.Create;
NativeWin.getDecorView.getDrawingRect(ContentRect) ;
FRealBounds := Rect(ContentRect.left, ContentRect.top, ContentRect.right, ContentRect.bottom);
end
else
FRealBounds := TRect.Empty;


end;


procedure THeaderFooterForm.HideUI;
begin
if FJEdit.getVisibility <> TJView.JavaClass.INVISIBLE then
begin
FJEdit.setVisibility(TJView.JavaClass.INVISIBLE);
FJNativeLayout.setPosition(FRealBounds.Right * 2 , FRealBounds.Height * 2);
end;
end;


procedure THeaderFooterForm.InitUIThread;
begin
FJEdit := TJEditText.JavaClass.init(TAndroidHelper.Activity) ;
FJNativeLayout := TJNativeLayout.JavaClass.init(TAndroidHelper.Activ ity,
MainActivity.getWindow.getDecorView.getWindowToken );
FJNativeLayout.setPosition(100,100);
FJNativeLayout.setSize(300,300);
FJNativeLayout.setControl(FJEdit);
FJEdit.setVisibility(TJView.JavaClass.VISIBLE);
end;


procedure THeaderFooterForm.ShowUI;
begin
FJNativeLayout.setPosition(FBounds.Left, FBounds.Top);
FJNativeLayout.setSize(FBounds.Right, FBounds.Bottom);
if FJEdit.getVisibility <> TJView.JavaClass.VISIBLE then
FJEdit.setVisibility(TJView.JavaClass.VISIBLE);
end;


procedure THeaderFooterForm.UpdateContentFromControl;
var
Pos: TPointF;
begin
while FJNativeLayout = nil do
Application.ProcessMessages;


if FJNativeLayout <> nil then
begin
Pos := Panel1.LocalToAbsolute(TPointF.Zero) * FScale;


FBounds := Rect(Round(Pos.X), Round(Pos.Y), Round(50 * FScale), Round(50 * FScale));
if Panel1.Visible and Panel1.ParentedVisible and
(TCommonCustomForm(Panel1.Root.GetObject)).Visible and
(TCommonCustomForm(Panel1.Root.GetObject)).Active then
ShowUI
else
HideUI;
end
else
HideUI;
end;