Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Android BackButton.. (https://www.delphipraxis.net/193413-android-backbutton.html)

erich.wanker 27. Jul 2017 11:04

Android BackButton..
 
Hallo Leute,

Delphi Tokyo / Android app

Ich will beim Andoid/BackButton nicht aus meiner AndroidApp geworfen werden, sondern mein "Hauptmenue" aufrufen, leider finde ich nix was funktioniert...

Alles was ich gefunden habe ist ähnlich wie mein Versuch: es comiliert schön - macht aber nix ;-)

Delphi-Quellcode:
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);

var FService : IFMXVirtualKeyboardService;
begin
  if Key = vkHardwareBack then
    begin
      TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
      if (FService <> nil) and (TVirtualKeyboardState.Visible in FService.VirtualKeyBoardState) then
        begin
            if ausrichtung='hoch' then maincontrol.ActiveTab:=menu;
            if ausrichtung='quer' then maincontrol.ActiveTab:=menu2;
            key:=0;
        end

      end;
end;

mensch72 27. Jul 2017 11:36

AW: Android BackButton..
 
Delphi-Quellcode:
procedure TfrmPscreen.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
var
  FService : IFMXVirtualKeyboardService;
begin
  if (Key = vkHardwareBack) or (Key = vkHome) then begin
    TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
    if (FService <> nil) and (TVirtualKeyboardState.Visible in FService.VirtualKeyBoardState) then
    begin
      // Back button pressed, keyboard visible, so do nothing...
    end else
    begin
      Key := 0;
      // Back button pressed, keyboard not visible or not supported on this platform, lets exit the app...
      MessageDlg('Exit Application?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1, OnCloseDialog);
    end;
    Exit;
  end;

  if Key = sgiUpRightLong then begin // Menu button pressed ?
    showmessage(verToolV1);
    Exit;
  end;

end;

procedure TfrmPscreen.OnCloseDialog(Sender: TObject; const AResult: TModalResult);
begin
  if AResult = mrOK then begin
    if Assigned(FrmQscreen) then FrmQscreen.Close;
    if Assigned(FrmPscreen) then FrmPscreen.Close;
    Close;
  end;
end;

da siehst du wie man die "SystemButtons" nutzt, auch der aktuell fast nicht mehr zufindende "Menu" Button ist abfragbar und hier einfach mal eine "VersionInfo"
der BackButton schließt hier nicht sofort, sondern erst wenn es der Nutzer ausdrücklich bestätigt... da kannste aber auch beliebg anderes abhängig vom AppState programmieren

Rollo62 27. Jul 2017 11:37

AW: Android BackButton..
 
Probier mal das ...

Delphi-Quellcode:
// Processes the KeyUp in Forms, to standard behaviour of Android Back key
function Form_KeyUp_Back(const AForm : TCustomForm; var Key: Word; var KeyChar: Char; Shift: TShiftState; bCanTerminate : Boolean) : Boolean;
{$IF DEFINED(ANDROID) or DEFINED(IOS) }
var
  FService : IFMXVirtualKeyboardService;
{$ENDIF DEFINED(ANDROID) or DEFINED(IOS) }
begin

  Result := False;

  //V3
  if Key = vkHardwareBack then
  begin

{$IF DEFINED(ANDROID) or DEFINED(IOS) }
    TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
    if (FService <> nil) and (TVirtualKeyboardState.Visible in FService.VirtualKeyBoardState) then
    begin
      // Back button pressed, keyboard visible, so do nothing...
    end else
    begin
      Key   := 0;
      Result := True;

      // Back button pressed, keyboard not visible or not supported on this platform,
      // lets exit the app... if condition is met
      // !! Mobile apps cannot be terminated
      if bCanTerminate then
      begin

      end;

    end;
{$ENDIF DEFINED(ANDROID) or DEFINED(IOS) }

  end;

end;

procedure TMain_Frm.FormKeyUp(Sender: TObject; var Key: Word;
  var KeyChar: Char; Shift: TShiftState);
begin

{$IFDEF ANDROID}
  if Form_KeyUp_Back(Self, Key, KeyChar, Shift, True) then
  begin
    if TabControlHeader.ActiveTab <> TabItemH1ViewEntry then
    begin

      ....

    end else
    begin
      // May Finish the main Form here, w/o closing it

      View_Hide;
    end;

    Exit;
  end;
{$ENDIF ANDROID}

  if Key = vkReturn then
  begin

  end;

end;
Rollo

erich.wanker 27. Jul 2017 13:06

AW: Android BackButton..
 
Hallo Leute :-)
vielen Dank .. funktioniert jetzt einwandfrei ...


Delphi-Quellcode:
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
var
  FService : IFMXVirtualKeyboardService;
begin
  if (Key = vkHardwareBack) or (Key = vkHome) then begin
    TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
    if (FService <> nil) and (TVirtualKeyboardState.Visible in FService.VirtualKeyBoardState) then
    begin
         // Tastatur ist eingeblendet .. also nix tun
    end
    else
    begin
      Key := 0;
      if (maincontrol.ActiveTab <> menu) AND (maincontrol.ActiveTab <> menu2) AND (maincontrol.ActiveTab <> login) AND (maincontrol.ActiveTab <> login2) then
      begin
            if ausrichtung='hoch' then maincontrol.ActiveTab:=menu;
            if ausrichtung='quer' then maincontrol.ActiveTab:=menu2;
      end
      else
      begin

            if (maincontrol.ActiveTab = menu) OR (maincontrol.ActiveTab = menu2) then
            begin
                  if ausrichtung='hoch' then maincontrol.ActiveTab:=login;
                  if ausrichtung='quer' then maincontrol.ActiveTab:=login2;
            end
            else
            begin
                   MessageDlg('Programm beenden?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1, OnCloseDialog);
            end;
      end;


    end;
    Exit;
  end;

  if Key = sgiUpRightLong then begin // Menu button pressed ?
    if ausrichtung='hoch' then maincontrol.ActiveTab:=menu;
    if ausrichtung='quer' then maincontrol.ActiveTab:=menu2;
  end;

end;



procedure TForm1.OnCloseDialog(Sender: TObject; const AResult: TModalResult);
begin
  if AResult = mrOK then begin
    Close;
  end;
end;

Rollo62 27. Jul 2017 13:56

AW: Android BackButton..
 
Ich weis nicht wie du deine Ausrichtung einstellst, womöglich über die System-Events.
Aber du könntest auch die aktuelle Einstellung abfragen:

Delphi-Quellcode:
function IsPortraitOrientation: Boolean;
var
  FScreenService: IFMXScreenService;
begin
  Result := true;
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, FScreenService) then
    Result := (FScreenService.GetScreenOrientation = TScreenOrientation.Portrait) or
      (FScreenService.GetScreenOrientation = TScreenOrientation.InvertedPortrait);
end;
Rollo

erich.wanker 27. Jul 2017 14:01

AW: Android BackButton..
 
Hallo Rollo62

ich hab in OnResize folgendes:

login und login2 sind TabSheets mit angepassten Buttons für den Loginbereich - horizontal/vertikal
menu und menu2 sind TabSheets mit angepassten Buttons für das Hauptmenü - horizontal/vertikal

Delphi-Quellcode:
procedure TForm1.FormResize(Sender: TObject);
var
  ScreenService: IFMXScreenService;
begin

  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) then
  begin
    if ScreenService.GetScreenOrientation in [TScreenOrientation.soPortrait, TScreenOrientation.soInvertedPortrait] then
    Begin
        if maincontrol.ActiveTab=menu2 then maincontrol.ActiveTab:=menu;
        if maincontrol.ActiveTab=login2 then maincontrol.ActiveTab:=login;
        ausrichtung:='hoch';
    End

  else

     begin
        if maincontrol.ActiveTab=menu then maincontrol.ActiveTab:=menu2;
        if maincontrol.ActiveTab=login then maincontrol.ActiveTab:=login2;
        ausrichtung:='quer';
    end;


  end;

end;

Rollo62 27. Jul 2017 14:11

AW: Android BackButton..
 
Man muss sich nicht auf OnResize verlassen, da gab es zumindest bei mir mal Probleme mit.
Auf den mobilen Platformen gibt es die Orientation-Events, die funktionieren bisher sehr gut.

Ist nur etwas lästig die immer zuzuordnen ...

Delphi-Quellcode:
procedure TS4View_Manager.OrientationChange_Subscribe;
begin

  // Register the Message handler
  //Subscribe to orientation change events in OnCreate or similar
  //
  FOrientationChangedId := TMessageManager.DefaultManager.SubscribeToMessage(
           TOrientationChangedMessage,
           EvOnOrientationChanged
         );

end;
Delphi-Quellcode:
procedure TS4View_Manager.EvOnOrientationChanged(const Sender: TObject;
   const Msg: TMessage);
var
  cmd : TS4Api_SysEvents;
  screenService : IFMXScreenService;

begin
  cmd := TS4Api_SysEvents.TApi.None;

  //
  // Evaluate the OrientationChange Event first
  //
  if Msg <> nil then
  begin

    if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService,
                                                         screenService) then
    begin

      case screenService.GetScreenOrientation of
        TScreenOrientation.Portrait        : cmd := TS4Api_SysEvents.OrientationChange_Portrait;
        TScreenOrientation.Landscape       : cmd := TS4Api_SysEvents.OrientationChange_Landscape;
        TScreenOrientation.InvertedPortrait : cmd := TS4Api_SysEvents.OrientationChange_PortraitInv;
        TScreenOrientation.InvertedLandscape: cmd := TS4Api_SysEvents.OrientationChange_LandscapeInv;
      end;

    end;

    screenService := nil; // Release the ServiceIntf

  end;

   .....

end;
Rollo

erich.wanker 27. Jul 2017 14:38

AW: Android BackButton..
 
super .. DANKE :-)

Werde ich mir anschauen

LiGrü
Erich


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:21 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz