React Native - TextInput

React Native - TextInput 首页 / React Native入门教程 / React Native - TextInput

TextInput是输入文本的基本组件。它有几个配置不同函数的道具,例如onChangeText,它接受一个函数,并在文本更改时调用它。 onSubmitEditing有一个函数,在提交文本时会调用该函数。

在此示例中,我们创建一个TextInput并执行一个onChangeText操作。每次更改文本时,它都会调用setState并检查拆分条件。

import React, { Component } from 'react';
import { AppRegistry, Text, TextInput, View } from 'react-native';

export default class PizzaTranslator extends Component {
    constructor(props) {
        super(props);
        this.state = {text: ''};
    }

    render() {
        return (
            <View style={{padding: 10}}>
                <TextInput
                    style={{height: 40,backgroundColor: 'azure', fontSize: 20}}
                    placeholder="Type here to translate!"
                    onChangeText={(text) => this.setState({text})}
                />
                <Text style={{padding: 100, fontSize: 50}}>
                    {this.state.text.split(' ').map((word) => word && '?').join(' ')}
                </Text>*
            </View>
        );
    }
}

输出

React Native Text InputReact Native Text Input

TextInput属性

allowFontScalingautoCapitalizeautoCorrectautoFocus
blurOnSubmitcaretHiddenclearButtonModeclearTextOnFocus
contextMenuHiddendataDetectorTypesdefaultValuedisableFullscreenUI
editableenablesReturnKeyAutomaticallyinlineImageLeftinlineImagePadding
keyboardAppearancekeyboardTypemaxLengthmultiline
numberOfLinesonBluronChangeonChangeText
onContentSizeChangeonEndEditingonFocusonKeyPress
onLayoutonScrollonSelectionChangeonSubmitEditing
placeholderplaceholderTextColorreturnKeyLabelreturnKeyType
scrollEnabledsecureTextEntryselectionselectionColor
selectionColorselectionStateselectTextOnFocusspellCheck
textContentTypestyletextBreakStrategyunderlineColorAndroid

多行TextInput

multiline 属性提供了输入多行文本。例如multiline = {true/false}。如果multiline = false,则属性borderButtomColor将不起作用。

import React, { Component } from 'react';
import { AppRegistry, View, TextInput } from 'react-native';

class UselessTextInput extends Component {
    render() {
        return (
            <TextInput
                {...this.props}//继承传递给它的任何道具;例如,下面的多行、numberOfLines
                editable = {true}
                maxLength = {40}
            />
        );
    }
}
export default class UselessTextInputMultiline extends Component {
    constructor(props) {
        super(props);
        this.state = {
            text: 'Useless Multiline Placeholder',
        };
    }

    
    render() {
        return (
            <View style={{
                backgroundColor: this.state.text,
                borderBottomColor: '#000000',
                borderBottomWidth: 1,
               }}
            >
                <UselessTextInput
                    multiline = {true}
                    numberOfLines = {10}
                    onChangeText={(text) => this.setState({text})}
                    value={this.state.text}
                    style={{fontSize: 20}}
                />
            </View>
        );
    }
}

输出

React Native Text InputReact Native Text Input

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

人工智能基础课 -〔王天一〕

从0开始学游戏开发 -〔蔡能〕

Go语言核心36讲 -〔郝林〕

Vim 实用技巧必知必会 -〔吴咏炜〕

A/B测试从0到1 -〔张博伟〕

云原生架构与GitOps实战 -〔王炜〕

结构沟通力 -〔李忠秋〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

Midjourney入门实践课 -〔Jovi〕

好记忆不如烂笔头。留下您的足迹吧 :)