我在Modal
内部有一个带有autoFocus={true}
属性的TextInput
。一旦打开Modal
,TextInput
会自动聚焦,但是键盘不会自动弹出。令人惊讶的是,这种情况是随机发生的。有时键盘会弹出,有时不会。这种情况在模拟器和真实设备上都会发生。
有什么建议可以克服这种问题吗?谢谢。
我在Modal
内部有一个带有autoFocus={true}
属性的TextInput
。一旦打开Modal
,TextInput
会自动聚焦,但是键盘不会自动弹出。令人惊讶的是,这种情况是随机发生的。有时键盘会弹出,有时不会。这种情况在模拟器和真实设备上都会发生。
有什么建议可以克服这种问题吗?谢谢。
<Modal onShow={() => {this.textInput.focus()}} >
<TextInput ref={input => {this.textInput = input}} />
</Modal>
我目前遇到了同样的问题。之前我使用了saumil和其他人提供的解决方案,但是针对函数组件进行了调整:
import React, { useRef } from 'react';
...
let textInput = useRef(null);
<Modal onShow={() => { textInput.focus(); }} ...>
<TextInput ref={(input) => { textInput = input; }} ... />
</Modal>
autoFocus={true}
非常重要,以获得一致的结果。这段代码对我来说是有效的。使用 android:focusedByDefault="true"
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:focusedByDefault="true" />
</com.google.android.material.textfield.TextInputLayout>
需要检查一下,
// 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);
}
}
});
focus
方法。像这样;focus() {
this.textInputRef.focus();
}
render() {
return (
<TextInput
ref={ref => {
this.textInputRef = ref;
}}
/>
);
}
focus
函数。它将聚焦于文本输入框并立即打开键盘。
focus()
方法吗? - firats