简体   繁体   English

如何在 Flutter 中为文本字段自动显示键盘

[英]How to show the Keyboard automatically for a Textfield in Flutter

I have a TextField in Flutter of which I want to automatically select the text and show the keyboard.我在 Flutter 中有一个TextField ,我想自动 select 文本并显示键盘。 I can select the text through a TextEditingController , but even with a FocusNodes requestFocus the keyboard isn't shown, when the Widget opens.我可以通过TextEditingController select 文本,但即使使用FocusNodes requestFocus,当小部件打开时,键盘也不会显示。

How to automatically open the keyboard for a TextField?如何自动打开 TextField 的键盘?

You can use the autofocus:true property of the TextField :您可以使用TextFieldautofocus:true属性:

Whether this text field should focus itself if nothing else is already focused.如果没有其他内容已经聚焦,此文本字段是否应该聚焦自身。

So whenever the widget appears on screen, if theres nothing else with the keyboard focus, the focus will automatically be directed to it, thus opening the keyboard.所以每当小部件出现在屏幕上时,如果没有其他键盘焦点,焦点将自动指向它,从而打开键盘。

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

You can set the autofocus property on TextField to true:您可以将 TextField 上的 autofocus 属性设置为 true:

TextField(
  autofocus: true,
);

Hope it helps!希望能帮助到你!

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,)


}

I have done this just using:我这样做只是使用:

autofocus: true,

But if you want more control over your TextField / TextFormFeild keyboard you can use:但是,如果您想更好地控制您的 TextField / TextFormFeild 键盘,您可以使用:

1. First declare a focus node object: 1.首先声明一个焦点节点object:

FocusNode focusNode = FocusNode();  // declear a focusNode object

2. On TextFeild / TextFormFeild, just do like below: 2. 在 TextFeild / TextFormFeild 上,只需执行以下操作:

    focusNode: focusNode,   // assign focusNode object on focusNode value
    autofocus: true,       // make autofocus true for first auto open keyboard

3. Just call this function when you want to open your keyboard: 3. 当你想打开你的键盘时,只需调用这个 function:

   void openKeyboard () {
       FocusScope.of(context).requestFocus(inputNode); 
    }

This is an example of how you can use it.这是一个如何使用它的示例。 Using that format you can open the keyboard automatically / you have complete control over whether or not you need to open the keyboard.使用该格式,您可以自动打开键盘/您可以完全控制是否需要打开键盘。

I hope this will fix your issue.我希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有 TextField 的情况下使用 KeyboardListener 在 Flutter/iOS 上显示键盘? - How do you show keyboard on Flutter/iOS with KeyboardListener without TextField? 自动显示 CupertinoSearchTextField() 的键盘 - Flutter - Automatically show keyboard for CupertinoSearchTextField() - Flutter Flutter:当焦点文本字段时键盘不显示 - Flutter:Keyboard not show when focus TextField 如何停止键盘弹出问题是 TextField flutter - How stop keyboard popping issue is TextField flutter 如何在 Flutter 上的键盘上堆叠 cursor 文本字段? - How to stack cursor textfield over keyboard on Flutter? 如何在textfield flutter中设置虚拟键盘的数据 - how set data of virtual keyboard in textfield flutter 如何在 Flutter 中专注于 TextField 但不显示键盘? - How to keep focus on TextField but not display keyboard in Flutter? 当文本字段集中在 flutter 时如何避免使用键盘 - how to avoid keyboard when textfield focused in flutter Flutter:如何在没有android底部导航栏聚焦文本字段的情况下显示屏幕键盘? - Flutter: How to show onscreen keyboard without android's bottom navigation bar on focusing a textfield? Flutter 如何在单击 TextField 时始终隐藏键盘但保持焦点(保持显示光标) - Flutter How to always hide keyboard when click on TextField but keep focus(Keep show cursor)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM