【问题标题】:How to show the Keyboard automatically for a Textfield in Flutter如何在 Flutter 中为文本字段自动显示键盘
【发布时间】:2020-06-16 00:33:06
【问题描述】:

我在 Flutter 中有一个 TextField,我想自动选择其中的文本并显示键盘。 我可以通过TextEditingController 选择文本,但即使使用FocusNodes requestFocus,当小部件打开时,键盘也不会显示。

如何自动打开TextField的键盘?

【问题讨论】:

    标签: flutter textfield


    【解决方案1】:

    您可以使用TextFieldautofocus:true 属性:

    如果没有其他内容已聚焦,则此文本字段是否应聚焦自身。

    所以每当小部件出现在屏幕上时,如果键盘焦点没有其他内容,焦点将自动指向它,从而打开键盘。

    TextField(TextEditingController: controller, 
             focusNode: focusNode,
             autofocus:true)
    

    【讨论】:

      【解决方案2】:

      您可以将 TextField 上的 autofocus 属性设置为 true:

      TextField(
        autofocus: true,
      );
      

      希望对你有帮助!

      【讨论】:

        【解决方案3】:
        class yourWidget extends StatelessWidget {  
        FocusNode inputNode = FocusNode();
        // to open keyboard call this function;
        void openKeyboard(){
        FocusScope.of(context).requestFocus(inputNode)
        }
        
        @override
        Widget build(BuildContext context) {
          TextFormField(
           //assign the  node like this
           focusNode: inputNode,
           autofocus:true,)
        
        
        }
        

        【讨论】:

        • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
        猜你喜欢
        • 2021-08-30
        • 2016-06-11
        • 1970-01-01
        • 2023-03-25
        • 2020-05-17
        • 2020-12-11
        • 1970-01-01
        • 2015-08-20
        • 1970-01-01
        相关资源
        最近更新 更多