Javascript 关闭键盘后禁用文本输入
使用React Native TextInput,当在键盘可见的情况下点击后退按钮(屏幕截图中的红色1)时,可以在稍后编辑TextInput值。但是,当点击检查/输入图标(屏幕截图中的红色2)时,键盘消失后无法更改文本输入。之后点击输入时,插入符号在点击过程中出现,但随后消失,什么也没有发生 这是我的组件:Javascript 关闭键盘后禁用文本输入,javascript,react-native,Javascript,React Native,使用React Native TextInput,当在键盘可见的情况下点击后退按钮(屏幕截图中的红色1)时,可以在稍后编辑TextInput值。但是,当点击检查/输入图标(屏幕截图中的红色2)时,键盘消失后无法更改文本输入。之后点击输入时,插入符号在点击过程中出现,但随后消失,什么也没有发生 这是我的组件: export class MyInput extends Component { constructor (props) { super(props);
export class MyInput extends Component {
constructor (props) {
super(props);
this.state = {
inputValue: `${Date.now()}-document`,
};
this.onFocus = this.onFocus.bind(this);
}
onFocus (text) {
if (this.placeholderRemoved) {
return text;
}
this.textInput.clear();
this.placeholderRemoved = true;
return '';
}
render () {
return (
<KeyboardAvoidingView>
<TextInput
value={this.state.inputValue}
onChangeText={(text) => {
this.setState({inputValue: text});
}}
/>
</KeyboardAvoidingView>
);
}
}
导出类MyInput扩展组件{
建造师(道具){
超级(道具);
此.state={
inputValue:`${Date.now()}-document`,
};
this.onFocus=this.onFocus.bind(this);
}
onFocus(文本){
如果(此占位符已删除){
返回文本;
}
this.textInput.clear();
this.placeholder=true;
返回“”;
}
渲染(){
返回(
{
this.setState({inputValue:text});
}}
/>
);
}
}
通过以下方式进行测试:
- 小米Pocopone F1,安卓8.1
- 谷歌像素2(模拟器),安卓7.1.1
import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
import {MyInput} from './myInputViewComponent';
export default class myApp extends Component {
render() {
return (
<View>
<MyInput />
</View>
);
}
}
AppRegistry.registerComponent('myApp', () => myApp);
import React,{Component}来自'React';
从“react native”导入{AppRegistry,View};
从“./myInputViewComponent”导入{MyInput};
导出默认类myApp扩展组件{
render(){
返回(
);
}
}
AppRegistry.registerComponent('myApp',()=>myApp);
“react”:“16.7.0”、“react native”:“0.57.6”,
使用模拟器或模拟器的软键盘。。您可以从硬件菜单中检查键盘属性并在那里搜索。我能够重现您的问题,而这确实是RN版本0.57.6
的问题。因此,在这种情况下,您的解决方案是使用RN版本0.57.7
或更高版本,这个问题应该得到解决
这一点得到进一步确认,因为0.57.6
的发布日志表明TextInput
中引入了as问题,该问题针对0.57.7
等进行了修复
参考资料:谢谢,但这也发生在真正的设备上(小米Pocopone F1)。我也不想让应用程序的用户有这样的问题:当你注释掉你的onFocus块时会发生什么?我觉得你的回报声明可能弄乱了什么?你能添加一段代码来说明
MyInput
组件是如何使用的吗?@SeanWang将文档中的内容复制并粘贴到my index.js中时也会遇到同样的问题,所以它不应该是onFocus块。你能添加你的父组件代码吗?对不起,我在谷歌Pixel 2(Emulator),Android 8.0上没有看到这个问题。这可能是一个新的RN错误?如果您仍然没有答案,我稍后会检查。注意:截至编写之日,最新版本为RN0.57.8
React16.6.3