[英]How do I make a TextInput show the start of the entered text instead of the end in Android?
我的 react-native 应用程序中有一个TextInput
。 当我设置值 prop 并且值比TextInput
的长度长时,它显示从末尾而不是开头的文本。 在 iOS 中它工作正常,这似乎是一个 Android 问题。 有没有办法将文本从TextInput
的开头或首选显示对齐?
这是我看到的:
这就是我想要的:
这是我的代码
<TextInput
defaultValue={address}
ref="searchInput"
autoCorrect={false}
style={searchStyles.searchInputField}
placeholderTextColor={'#ddd'}
placeholder="Location"
onChangeText={query => this.search(query)}
/>
添加selection={{start:0}}
不带end
属性
试试这个对我有用
constructor(props) {
super(props);
this.state = {
selection: Platform.OS === 'android' ? { start: 0 } : null
}
}
onFocus =()=>{
if(Platform.OS==='android'){
this.setState({ selection:null });
}
}
onBlur =()=>{
if(Platform.OS==='android'){
this.setState({ selection:{ start: 0, end: 0 } });
}
}
<TextInput
onFocus={()=>this.onFocus()}
onBlur={()=>this.onBlur()}
selection={this.state.selection}
/>
this.inputRef &&
this.inputRef.setNativeProps({ selection: { start: 0, end: 0 } })
将代码放在 onEndEditing 中以及您想要更新文本的任何位置。
或者同时更新文本和选择:
this.inputRef &&
this.inputRef.setNativeProps({ text: value, selection: { start: 0, end: 0 } })
ellipsizeMode="head"
或者你可以试试ellipsizeMode="tail"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.