首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >键盘不会自动打开,尽管autoFocus={true}

键盘不会自动打开,尽管autoFocus={true}
EN

Stack Overflow用户
提问于 2020-07-23 05:15:04
回答 4查看 1.2K关注 0票数 3

我在带有TextInputModal中有一个autoFocus={true}。一旦打开ModalTextInput就会自动聚焦,但键盘并没有打开automatically.And,这是随机发生的。有时打开键盘,有时不打开键盘。这在模拟器和真实设备中都有发生。

有什么建议可以克服这种行为吗?谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-07-23 07:37:34

您可以在任何时候使用引用将焦点传递给TextInput。

代码语言:javascript
复制
<Modal onShow={() => {this.textInput.focus()}} > 
    <TextInput ref={input => {this.textInput = input}} />
</Modal>
票数 1
EN

Stack Overflow用户

发布于 2020-07-23 10:39:02

我现在也有同样的问题。我以前使用过saumil等人提出的解决方案建议,但根据功能组件进行了调整:

代码语言:javascript
复制
import React, { useRef } from 'react';
...
let textInput = useRef(null);
<Modal onShow={() => { textInput.focus(); }} ...>
   <TextInput ref={(input) => { textInput = input; }} ... />
</Modal>

这很管用,但我不太清楚我在做什么(欢迎解释)。删除autoFocus={true}以获得一致的结果是很重要的。

票数 1
EN

Stack Overflow用户

发布于 2020-07-23 05:20:22

不过,需要检查一下,

代码语言:javascript
复制
// Keyboard IpM
 InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);

// input is TextInputEditText in here.
// adding a listener 
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean isFocused) {

        if (isFocused) {
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        } 
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63047312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档