gpt4 book ai didi

javascript - React Native - 显示键盘并渲染其值而不使用输入

转载 作者:行者123 更新时间:2023-12-01 00:43:41 26 4
gpt4 key购买 nike

有没有办法只使用键盘而不输入任何文本并在更改时获取其值?

我想仅在按钮单击事件上显示键盘,并在没有任何输入的情况下在 View 中呈现其键入值。

实现这个的正确方法是什么?

最佳答案

您可以添加虚拟 TextInput 和 onPress 按钮,将焦点设置在 TextInput 上以显示键盘。使用“onChangeText”属性保存状态并在 View 中显示

完整代码

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>
);
}
}

应用预览

enter image description here

关于javascript - React Native - 显示键盘并渲染其值而不使用输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57547218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com