[英]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.