You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FMX ListView搜索栏虚拟键盘默认显示数字视图的设置方法

FMX ListView内置搜索栏键盘默认视图调整方案

核心需求可行性说明

FMX框架的虚拟键盘配置通过KeyboardType属性映射各移动端系统的原生键盘类型,框架本身没有暴露「全字母键盘默认停留数字Tab」的独立控制接口——系统原生全键盘的默认分页是系统级默认逻辑,FMX未开放对应配置项。
你之前使用TVirtualKeyboardType.NumberPad的方案会唤起纯数字输入盘,没有切换到字母布局的入口,不符合保留全键盘的需求,可通过调整键盘类型枚举值直接实现目标效果,无需额外添加切换按钮。

最优实现方案(默认显示数字页、保留全字母切换能力)

将内置搜索框的KeyboardType属性设置为TVirtualKeyboardType.NumbersAndPunctuation即可,该类型键盘的平台表现为:

  • 键盘唤起时默认展示数字、常用标点输入区
  • 自带布局切换按钮,用户可随时手动切换到全字母输入布局
  • 安卓、iOS双平台表现一致,无需单独写平台适配代码

兼容所有FMX版本的实现代码

你原有遍历第一层子控件查找搜索框的逻辑存在版本兼容问题:高版本Delphi中ListView的内置搜索框会嵌套在SearchEditWrapper容器下,仅遍历第一层控件会找不到实例,建议用递归遍历子控件的方式查找,完整代码如下:

// 递归查找指定类型子控件的公共方法
function FindChildControl(AParent: TFmxObject; AClassType: TClass): TFmxObject;
var
  i: Integer;
begin
  Result := nil;
  for i := 0 to AParent.ChildrenCount - 1 do
  begin
    if AParent.Children[i].ClassType = AClassType then
      Exit(AParent.Children[i]);
    Result := FindChildControl(AParent.Children[i], AClassType);
    if Assigned(Result) then Exit;
  end;
end;

procedure TOutput.FormShow(Sender: TObject);
var
  LSearchBox: TSearchBox;
begin
  LSearchBox := TSearchBox(FindChildControl(lvInventory, TSearchBox));
  if Assigned(LSearchBox) then
  begin
    // 替换原有的NumberPad类型,实现默认数字页+可切字母的效果
    LSearchBox.KeyboardType := TVirtualKeyboardType.NumbersAndPunctuation;
  end;
end;

备选方案:手动按钮切换键盘模式

如果需要更灵活的键盘模式切换能力,可按以下步骤实现:

  1. 将查找到的TSearchBox实例存为窗体级私有变量,避免每次触发事件时重复遍历控件
  2. 在窗体上添加对应切换按钮,点击时动态修改KeyboardType属性
  3. 修改属性前如果搜索框已聚焦,先重置焦点再重新聚焦,确保键盘立即重载新布局
    参考实现代码:
// 窗体私有变量声明
private
  FInventorySearchBox: TSearchBox;

// 切换到纯数字键盘按钮事件
procedure TOutput.btnSwitchNumClick(Sender: TObject);
begin
  if not Assigned(FInventorySearchBox) then Exit;
  if FInventorySearchBox.IsFocused then
    FInventorySearchBox.ResetFocus;
  FInventorySearchBox.KeyboardType := TVirtualKeyboardType.NumberPad;
  FInventorySearchBox.SetFocus;
end;

// 切换回全字母键盘按钮事件
procedure TOutput.btnSwitchAlphaClick(Sender: TObject);
begin
  if not Assigned(FInventorySearchBox) then Exit;
  if FInventorySearchBox.IsFocused then
    FInventorySearchBox.ResetFocus;
  FInventorySearchBox.KeyboardType := TVirtualKeyboardType.Default;
  FInventorySearchBox.SetFocus;
end;

注意:Delphi 10.2及以下低版本中NumbersAndPunctuation枚举值在iOS平台可能映射为纯数字标点盘,无字母切换入口,该场景下可使用备选按钮切换方案。


内容的提问来源于stack exchange,提问作者user1937012

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 05:27:45