[英]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 :您可以使用TextField的
autofocus: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.