我在带有TextInput的Modal中有一个autoFocus={true}。一旦打开Modal,TextInput就会自动聚焦,但键盘并没有打开automatically.And,这是随机发生的。有时打开键盘,有时不打开键盘。这在模拟器和真实设备中都有发生。
有什么建议可以克服这种行为吗?谢谢。
发布于 2020-07-23 07:37:34
您可以在任何时候使用引用将焦点传递给TextInput。
<Modal onShow={() => {this.textInput.focus()}} >
<TextInput ref={input => {this.textInput = input}} />
</Modal>发布于 2020-07-23 10:39:02
我现在也有同样的问题。我以前使用过saumil等人提出的解决方案建议,但根据功能组件进行了调整:
import React, { useRef } from 'react';
...
let textInput = useRef(null);
<Modal onShow={() => { textInput.focus(); }} ...>
<TextInput ref={(input) => { textInput = input; }} ... />
</Modal>这很管用,但我不太清楚我在做什么(欢迎解释)。删除autoFocus={true}以获得一致的结果是很重要的。
发布于 2020-07-23 05:20:22
不过,需要检查一下,
// 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);
}
}
});https://stackoverflow.com/questions/63047312
复制相似问题