gpt4 book ai didi

reactjs - 如何在 react-native 组件中关注下一个输入字段

转载 作者:行者123 更新时间:2023-12-03 20:17:14 25 4
gpt4 key购买 nike

我有一个名为 ImageTextField 的组件:

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

export default class ImageTextField extends React.Component {
render() {
const {
placeHolderValue, value, editable
} = this.props;
return (
<View>
<TextInput
placeholder={placeHolderValue}
placeholderTextColor="#b1b1b1"
value={value}
editable={editable}
/>
</View>
);
}
}

我的屏幕:
import React from 'react';
import { StyleSheet, View, Text, TouchableOpacity} from 'react-native';
import ImageTextField from '../../custom-elements/ImageTextField';

export default class App extends React.Component {
render() {
return (
<ImageTextField
onChangeText={text => this.onNameChange(text)}
color={Colors.textFieldBlack}
placeHolderValue="Name"
value={this.state.name}
autoCapitalize="words"
/>
<ImageTextField
onChangeText={text => this.onEmailChange(text)}
color={Colors.textFieldBlack}
placeHolderValue="E-mail"
value={this.state.email}
keyboardType='email-address'
/>
<ImageTextField
onChangeText={text => this.onPhoneChange(text)}
color={Colors.textFieldBlack}
placeHolderValue="Phone"
value={this.state.phone}
keyboardType="phone-pad"
/>
);
}
}

在上面的代码中,我有我在屏幕中使用的 ImageTextField 组件,我想聚焦下一个字段,如果用户按下键盘中的下一个键,则需要跳转到第二个字段。

最佳答案

添加一个 Prop ,它将检索输入的 ref 到您的 ImageTextField 组件。所以你的渲染函数应该是这样的:

render() {
const {
placeHolderValue, value, editable, inputRef
} = this.props;
return (
<View>
<TextInput
placeholder={placeHolderValue}
placeholderTextColor="#b1b1b1"
value={value}
editable={editable}
ref={inputRef}
/>
</View>
);
}

然后您的 App 组件将如下所示:
render() {
return (
<ImageTextField
onChangeText={text => this.onNameChange(text)}
color={Colors.textFieldBlack}
placeHolderValue="Name"
value={this.state.name}
autoCapitalize="words"
onSubmitEditing={() => this.emailAddress.focus()}
/>
<ImageTextField
onChangeText={text => this.onEmailChange(text)}
color={Colors.textFieldBlack}
placeHolderValue="E-mail"
value={this.state.email}
keyboardType='email-address'
inputRef={ref => this.emailAddress = ref}
onSubmitEditing={() => this.phoneNumber.focus()}
/>
<ImageTextField
onChangeText={text => this.onPhoneChange(text)}
color={Colors.textFieldBlack}
placeHolderValue="Phone"
value={this.state.phone}
keyboardType="phone-pad"
inputRef={ref => this.phoneNumber = ref}
/>
);
}

关于reactjs - 如何在 react-native 组件中关注下一个输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49730978/

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