說明:XE5 在 Android 平台上存在這一些鍵盤操作的問題,目前發現有下列幾種:
- 按鍵盤上的隱藏鍵后,無法按上一頁(需要修改 XE5 源碼「FMX.VirtualKeyboard.Android.pas」)。
- 按鍵盤上的隱藏鍵后,無法觸發 OnVirtualKeyboardHidden 事件。
- 關閉鍵盤后,再點一次無法顯示鍵盤(大部份機子都有這個問題,實測 Nexus 7 II 沒有這個問題)。
適用:Delphi XE5 update 2
參考:此修正方法是參考「完美解決 XE5 安卓下 虛擬鍵盤 隱藏沒有事件的 BUG。」在此感謝。
目前發現的鍵盤問題,需要修改 XE5 源碼「FMX.VirtualKeyboard.Android.pas」才能修正,改好的檔案已經放在上面源碼 zip 里,改動內容如下({+++>}內為增加的部份{<+++}):
FMX.VirtualKeyboard.Android.pas
{+++>} function ObtainKeyboardRect: TRect; var ContentRect, TotalRect: JRect; begin ContentRect := TJRect.Create; TotalRect := TJRect.Create; MainActivity.getWindow.getDecorView.getWindowVisibleDisplayFrame(ContentRect); MainActivity.getWindow.getDecorView.getDrawingRect(TotalRect); Result := TRectF.Create(ConvertPixelToPoint(TPointF.Create(TotalRect.left, TotalRect.top + ContentRect.height)), ConvertPixelToPoint(TPointF.Create(TotalRect.right, TotalRect.bottom))).Truncate; end; {<+++} function TVirtualKeyboardAndroid.GetVirtualKeyBoardState: TVirtualKeyBoardState; begin if FError then Result := [vksError] else Result := []; if IsAutoShow then Result := Result + [vksAutoShow]; if not FError then begin {+++>} // 這里只解決: 按鍵盤上的隱藏后, 無法再按上一頁的問題 (按上一頁后, 才會執行這里). if FState = vkbsVisible then if ObtainKeyboardRect.Height < 30 then SetState(TVirtualKeyboardAndroid.TvkbState.vkbsHidden); {<+++} if FState = vkbsVisible then Result := Result + [vksVisible]; end; end;
Main.pas
//------------------------------------------------------------------------------ // 2014.04.02 by 龜山阿卍 QQ 1467948783 - // http://www.cnblogs.com/onechen/ - // - // 需修改 - // FMX.VirtualKeyboard.Android.pas - //------------------------------------------------------------------------------ unit Main; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Platform, FMX.VirtualKeyboard.Android, FMX.VirtualKeyboard, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Edit, FMX.StdCtrls, FMX.Layouts, FMX.Memo; type TForm1 = class(TForm) ToolBar1: TToolBar; Label1: TLabel; VirtualKeyboardTimer: TTimer; Memo1: TMemo; Edit1: TEdit; procedure FormVirtualKeyboardHidden(Sender: TObject; KeyboardVisible: Boolean; const Bounds: TRect); procedure FormVirtualKeyboardShown(Sender: TObject; KeyboardVisible: Boolean; const Bounds: TRect); procedure VirtualKeyboardTimerTimer(Sender: TObject); procedure Edit1Click(Sender: TObject); private { private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.fmx} // 顯示鍵盤 function ShowVirtualKeyboard(const AControl: TFmxObject): Boolean; var Service1: IFMXVirtualKeyboardService; begin if (ObtainKeyboardRect.Height < 30) and TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(Service1)) then Result := Service1.ShowVirtualKeyboard(AControl) else Result := False; end; procedure TForm1.FormVirtualKeyboardHidden(Sender: TObject; KeyboardVisible: Boolean; const Bounds: TRect); begin Edit1.Text := 'Keyboard hidden!'; Memo1.Text := Format('W: %d H: %d', [Bounds.Width, Bounds.Height]); VirtualKeyboardTimer.Enabled := False; // 關閉檢查鍵盤高度 end; procedure TForm1.FormVirtualKeyboardShown(Sender: TObject; KeyboardVisible: Boolean; const Bounds: TRect); begin Edit1.Text := 'Keyboard shown!'; Memo1.Text := Format('W: %d H: %d', [Bounds.Width, Bounds.Height]); VirtualKeyboardTimer.Enabled := True; // 開啟檢查鍵盤高度 end; procedure TForm1.VirtualKeyboardTimerTimer(Sender: TObject); var R: TRect; begin // 檢查鍵盤高度 < 30 代表鍵盤關閉. R := ObtainKeyboardRect; if R.Height < 30 then Self.OnVirtualKeyboardHidden(Sender, False, R); end; procedure TForm1.Edit1Click(Sender: TObject); begin // 在 TEdit 或 TMemo 需要用到鍵盤的控件, 加入下行, 才能確保關閉鍵盤后, 再點一次可以顯示鍵盤. ShowVirtualKeyboard(TFmxObject(Sender)); end; end.
「鍵盤上的隱藏鍵」是指下圖所示位置: