允许键盘输入到嵌入FireMonkey TPopup内的FireMonkey TEdit

时间:2014-01-02 19:31:37

标签: delphi firemonkey delphi-xe5

我很难将FireMonkey TEdit嵌套在FireMonkey TPopup内以接收键盘输入。适用于桌面和移动项目,虽然后者是我感兴趣的:

  1. 创建一个新的FMX项目。

  2. 向表单添加TButtonTPopup,向TEdit添加TPopup

  3. 将弹出式窗口的Placement属性设置为plCenter,将PlacementTarget设置为Button1

  4. 通过将弹出式窗口的OnClick属性设置为IsOpen来处理按钮的True事件。

  5. 运行项目,单击/点击按钮,然后尝试在编辑控件中输入文本。

  6. 有什么想法吗?正确答案当然可能是:不支持键盘输入,但documentation没有任何说法。

3 个答案:

答案 0 :(得分:2)

有同样的问题,但修复了很少覆盖的行为。将表单更改为TFormStyle.Normal并处理OnDeactivate事件。

    TPopup2 = class(TPopup)

    protected
      function CreatePopupForm: TFmxObject; override;
    end;

    TPopupForm2 = class(TCustomPopupForm)

    private
      procedure OnDeactivateEvent(Sender: TObject);
    public
      constructor CreateNew(AOwner: TComponent; Dummy: NativeInt = 0); override;
    end;

function TPopup2.CreatePopupForm: TFmxObject;
var
  NewStyle: TStyleBook;
  NewForm: TPanelForm;
begin
  NewForm := nil;
  try
    if not Assigned(StyleBook) and Assigned(Scene) then
      NewStyle := Scene.GetStyleBook
    else
      NewStyle := StyleBook;
    NewForm := TPopupForm2.Create(Self, NewStyle, PlacementTarget);
  except
    FreeAndNil(NewForm);
    Raise;
  end;
  Result := NewForm;
end;

constructor TPopupForm2.CreateNew(AOwner: TComponent; Dummy: NativeInt);
begin
  inherited;
  BeginUpdate;
  try
    FormStyle := TFormStyle.Normal;
    BorderStyle := TFmxFormBorderStyle.None;
    Fill.Kind := TBrushKind.None;
    Transparency := True;
    OnDeactivate := OnDeactivateEvent;
  finally
    EndUpdate;
  end;

end;

procedure TPopupForm2.OnDeactivateEvent(Sender: TObject);
begin
  Close;
end;

答案 1 :(得分:1)

使用Popup1.popup(true)而不是更改isOpen属性

答案 2 :(得分:1)

键盘输入似乎不适用于TPopup。 一个简单的解决方案是使用TForm作为弹出窗体:

unit Popup;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, FMX.Edit;

type
  TfmPopup = class(TForm)
    Edit1: TEdit;
    Panel1: TPanel;
    procedure FormDeactivate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
  private
  protected
  public
  end;

var
  fmPopup: TfmPopup;

implementation

{$R *.fmx}

procedure TfmPopup.FormDeactivate(Sender: TObject);
begin
  Close;
end;

procedure TfmPopup.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  if Key = vkEscape then begin
    Close;
  end;
end;

end.

表单资源:

object fmPopup: TfmPopup
  Left = 0
  Top = 0
  BorderStyle = None
  Caption = 'Form1'
  ClientHeight = 94
  ClientWidth = 142
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop, iPhone, iPad]
  OnDeactivate = FormDeactivate
  OnKeyDown = FormKeyDown
  DesignerMobile = False
  DesignerWidth = 0
  DesignerHeight = 0
  DesignerDeviceName = ''
  DesignerOrientation = 0
  DesignerOSVersion = ''
  object Panel1: TPanel
    Align = Client
    Height = 94.000000000000000000
    Width = 142.000000000000000000
    TabOrder = 1
    object Edit1: TEdit
      Touch.InteractiveGestures = [LongTap, DoubleTap]
      TabOrder = 1
      Position.X = 20.000000000000000000
      Position.Y = 32.000000000000000000
      Width = 100.000000000000000000
      Height = 22.000000000000000000
    end
  end
end

当然你可以改进这个简单的例子: 不要将TEdit放在此表单上,而是继承此表单并将编辑放在那里。 E.g:

TfmMyPopup = class(TfmPopup)
  Edit1: TEdit;
private
protected
public
end;

使用TPopup等一些功能改进TfmPopup的基类: 例如。放置。 您可以使用TfmPopup中未显示的TPopup来使用TPopup的放置例程而无需重写此代码。