值控制属性
<TextInput
value="文本内容"
defaultValue="默认值"
onChangeText={(text) => console.log(text)}
onChange={(event) => console.log(event.nativeEvent.text)}
onFocus={() => console.log('获得焦点')}
onBlur={() => console.log('失去焦点')}
onSubmitEditing={(event) => console.log('提交')}
onEndEditing={(event) => console.log('结束编辑')}
/>
外观属性
<TextInput
style={styles.input}
placeholder="请输入..."
placeholderTextColor="#999"
textAlign="left"
textAlignVertical="center"
selectionColor="#007AFF"
/>
键盘属性
<TextInput
keyboardType="default"
keyboardAppearance="light"
returnKeyType="done"
returnKeyLabel="完成"
enablesReturnKeyAutomatically={true}
/>
输入控制属性
<TextInput
autoCapitalize="none"
autoCorrect={false}
autoComplete="off"
textContentType="none"
secureTextEntry={false}
multiline={false}
numberOfLines={1}
maxLength={100}
editable={true}
selection={{ start: 0, end: 0 }}
selectTextOnFocus={false}
contextMenuHidden={false}
blurOnSubmit={false}
caretHidden={true}
editable={false}
/>
方法
const inputRef = useRef<TextInput>(null);
inputRef.current?.focus();
inputRef.current?.blur();
inputRef.current?.clear();
inputRef.current?.isFocused();
inputRef.current?.setSelection(
start: number,
end?: number
);
事件处理
<TextInput
onKeyPress={({ nativeEvent }) => {
console.log('按键:', nativeEvent.key);
}}
onSelectionChange={({ nativeEvent }) => {
console.log('选择范围:', nativeEvent.selection);
}}
onContentSizeChange={({ nativeEvent }) => {
console.log('内容大小:', nativeEvent.contentSize);
}}
onScroll={({ nativeEvent }) => {
console.log('滚动位置:', nativeEvent.contentOffset);
}}
/>
平台特定属性
<TextInput
clearButtonMode="never"
clearTextOnFocus={false}
dataDetectorTypes="none"
autoComplete="off"
textAlignVertical="center"
underlineColorAndroid="transparent"
/>
样式属性
const styles = StyleSheet.create({
input: {
height: 40,
width: '100%',
padding: 10,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 5,
fontSize: 16,
fontFamily: 'System',
color: '#000',
backgroundColor: '#fff',
shadowColor: '#000',
elevation: 2,
}
});