Skip to main content

处理文本输入

TextInput核心组件,允许用户输入文本。它有一个 onChangeText 属性,每次文本更改时都会调用一个函数,还有一个 onSubmitEditing 属性,它在提交文本时调用一个函数。

¥TextInput is a Core Component that allows the user to enter text. It has an onChangeText prop that takes a function to be called every time the text changed, and an onSubmitEditing prop that takes a function to be called when the text is submitted.

例如,假设当用户键入时,你将他们的单词翻译成另一种语言。在这种新语言中,每个单词的书写方式都是相同的:🍕 因此句子 "你好,鲍勃" 将被翻译为 "🍕 🍕 🍕"。

¥For example, let's say that as the user types, you're translating their words into a different language. In this new language, every single word is written the same way: 🍕. So the sentence "Hello there Bob" would be translated as "🍕 🍕 🍕".

在此示例中,我们将 text 存储在状态中,因为它随时间变化。

¥In this example, we store text in the state, because it changes over time.

你可能还想通过文本输入做更多的事情。例如,你可以在用户键入时验证其中的文本。有关更详细的示例,请参阅 受控组件上的 React 文档TextInput 的参考文档

¥There are a lot more things you might want to do with a text input. For example, you could validate the text inside while the user types. For more detailed examples, see the React docs on controlled components, or the reference docs for TextInput.

文本输入是用户与应用交互的方式之一。接下来我们看看另一种类型的输入和 学习如何处理触摸

¥Text input is one of the ways the user interacts with the app. Next, let's look at another type of input and learn how to handle touches.