如何在Flutter中更改键盘的文本输入操作按钮(返回/回车键)?

110
在Android和iOS中,可以将键盘的回车/换行键更改为例如“Go”按钮(以及其他选项)。

illustration

在{{顶部}},我们可以看到两个系统上常规的“{{返回}}”按钮,这是在Android和iOS原生以及Flutter中默认没有修改的按钮。
在{{下面}},有另一个设置,在两个系统中都可以简单地在本机应用程序中进行调整。这种情况下是“{{前往}}”按钮。

可能是重复问题:https://dev59.com/j4vda4cB1Zd3GeqPWDBE,但与之不同,我没有投票。我不认为你的问题与Flutter有关。你只是想知道如何更改“Enter”的标题。 - Stephen J
1
@StephenJ,您链接的问题仅适用于原生iOS,而不是Flutter,我也不确定这是否与IME有关。 - creativecreatorormaybenot
1
啊,我以为它更像是“像Flutter一样”,而不是字面上的“在”,误解了。 - Stephen J
7个回答

195

TextField(或 TextFormField)的 输入操作 可以像这样指定(这里是 Go 按钮):

TextField(
  textInputAction: TextInputAction.go
  ...
)

所有可用输入操作的列表。


2
看起来可能已经完成了:https://github.com/flutter/flutter/pull/18855。 - Duncan Jones
3
物理键盘怎么样?我尝试了这个,但是在用户从物理键盘按下回车时它不起作用。 - Rutvik Gumasana
5
该解决方案适用于安卓系统。对于 iOS 系统,它只适用于 TextInputType.text 类型,而不是 TextInputType.number 类型。 - Tanmay Pandharipande
感谢您保持此答案的最新更新。 - rekire

86

这是你如何使用textInputAction

TextField(
  textInputAction: TextInputAction.search,
  onSubmitted: (value) {
    print("search");
  },
  decoration: InputDecoration(
    border: InputBorder.none,
    prefixIcon: Icon(Icons.search),
    hintText: 'Search ',
    contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
  ),
);

8
以下是如何添加操作按钮并监听onTap的方法:

对于TextField:

TextField(
  textInputAction: TextInputAction.go,
  onSubmitted: (value) {
    print("Go button is clicked");
  },
  decoration: InputDecoration(
    border: InputBorder.none,
    contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
  ),
);

对于TextFormField

TextFormField(
      textInputAction: TextInputAction.go,
      onFieldSubmitted: (value) {
      print("Go button is clicked");
      },
      decoration: const InputDecoration(
          hintText: "Type your search here",
          hintStyle: TextStyle(color: Colors.black26),
          filled: true,
          fillColor: Colors.white,
          border: OutlineInputBorder(
            borderSide: BorderSide.none,
            borderRadius: BorderRadius.all(Radius.circular(40.0)),
          ),
          contentPadding:
              EdgeInsets.symmetric(horizontal: 20.0, vertical: 16.0)),
    )

7
目前还无法实现此功能。 不过,您可以轻松地编辑Flutter源代码以使其成为可能。
需要进行以下编辑:
flutter/lib/src/widgets/editable_text.dart
将第430行的 _openInputConnection 更改为:
void _openInputConnection() {
  if (!_hasInputConnection) {
    final TextEditingValue localValue = _value;
    _lastKnownRemoteTextEditingValue = localValue;
    _textInputConnection = TextInput.attach(this,
        new TextInputConfiguration(
            inputType: widget.keyboardType,
            obscureText: widget.obscureText,
            autocorrect: widget.autocorrect,
            inputAction: widget.keyboardType == TextInputType.multiline
                ? TextInputAction.newline
                : TextInputAction.done
        )
    )..setEditingState(localValue);
  }
  _textInputConnection.show();
}

在同一个文件中,还需要在EditableText类(不是状态类)的~280行处声明一个新字段。
final TextInputAction textInputAction;

在164行之前的EditableText构造函数中分配它。
this.textInputAction,

同样的情况。但是要添加一个新字段,而不是到TextField中:
final TextInputAction textInputAction;

并将以下内容添加到它的构造函数中:

this.textInputAction,

最后,将新字段作为参数传递到EditableText的第479行:
    textInputAction: widget.textInputAction,

完成了。

现在你可以在你的应用程序中指定一个自定义的 TextInputAction。这不会破坏现有的 TextField,它只是添加了覆盖默认行为的能力。

new TextField(
  keyboardType: TextInputType.text,
  textInputAction: TextInputAction.newline,
),

只是为了明确一点,我没有找到解决方案。因此,我的应用程序中没有“Go”按钮。 - creativecreatorormaybenot
那段代码允许你分别设置键盘类型(电子邮件、电话号码等)和回车按钮。但最终,在Flutter中只有两个回车图标可用:换行符和勾选图标。 - Rémi Rousselet
是的,我有一段时间前就意识到了。这就是为什么我在问问题。我的悬赏信息甚至表明我只是在寻找解决方案。你只是说用你的方法我无法得到“Go”按钮。我相信有方法,也许只能本地实现。 - creativecreatorormaybenot
这可能有点复杂,因为Flutter引擎位于Flutter存储库之外。最好的方法是创建一个问题列表,列出所需的图标。 - Rémi Rousselet
不完全如此。这段代码添加了更改返回按键图标的功能,这是您回答所需的步骤。缺失的部分是在Flutter引擎内进行编辑以支持TextInputAction.go - Rémi Rousselet
显示剩余4条评论

5
基本上我们使用两种文本输入框,TextFieldTextFormField
对于 TextField
TextField(
  textInputAction: TextInputAction.go
  .......
)

对于TextFormField,

TextFormField(
          textInputAction: TextInputAction.go,
           ........
)

两者都有textInputAction属性,我们可以使用它


2

任何寻找 UIReturnKeyDone 的人都像这样:

TextField(
  textInputAction: TextInputAction.done
  ...
)

1

我还没有探索TextInputType的所有"keyboardType"选项,它是TextField的一个可选参数。

但是对于'emailAddress','datetime'和'phone',有一些明显不同的键盘选项 - 其中一个选项可能会发出您正在寻找的键盘...


1
我不是在寻找整个键盘布局的更改,而只是针对动作按钮的调整。 - creativecreatorormaybenot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接