Alw*_*sed 1 javascript react-native
有没有办法在没有任何文本输入的情况下使用键盘并在更改时获取其值?
我想仅在按钮单击事件上显示键盘,并在没有任何输入的情况下在视图中呈现其键入值。
什么是实现这一点的正确方法?
小智 5
您可以添加一个虚拟 TextInput 和 onPress 按钮将焦点设置在 TextInput 上以显示 keyboard 。使用“onChangeText”道具保存状态并显示在视图中
完整代码
import React from "react";
import { View, Text, Button, TextInput } from "react-native";
export default class App extends React.Component {
state = { text: "" };
render() {
return (
<View style={{ flex: 1, marginTop: 50 }}>
<TextInput
style={{ height: 0, width: 0, borderWidth: 0 }}
onChangeText={text => this.setState({ text })}
ref={ref => {
this.textInput = ref;
}}
autoCapitalize="none"
autoCorrect={false}
autoFocus={false}
/>
<Button
onPress={() => {
this.textInput.focus();
}}
title="Press Me To show Keyboard"
color="#841584"
/>
<View
style={{
borderColor: "red",
borderWidth: 1,
padding: 16,
marginTop: 20
}}
>
<Text style={{ marginBottom: 8 }}>Show Typing Values:</Text>
<Text>{this.state.text}</Text>
</View>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
应用预览
| 归档时间: |
|
| 查看次数: |
702 次 |
| 最近记录: |