是否有一种以编程方式突出显示/选择TextInput组件中的文本的方法?
发布于 2017-06-23 10:42:30
实际上,您可以通过参考文献访问textInput的方法。
<TextInput ref={input => this.myInput = input} selectTextOnFocus style={{height: 100, width: 100}} defaultValue='Hey there' />
以及您希望以编程方式选择所有文本的位置,您可以
this.myInput.focus()
在iOS上工作,对安卓系统不确定。
参考资料:http://facebook.github.io/react-native/releases/0.45/docs/textinput.html#selectionstate
发布于 2017-06-23 10:21:04
您可以使用selectTextOnFocus来实现这一点。这将确保在挖掘字段时突出显示TextInput中的所有文本。
发布于 2017-06-23 10:50:44
我不知道是否有更好的办法,但我找到了解决办法。案文必须首先突出重点。下面是一个例子
import React { Component } from 'react';
import { Button, TextInput, findNodeHandle } from 'react-native';
import TextInputState from 'react-native/lib/TextInputState';
class MyComponent extends Component {
render() {
return (
<View style={{ flex: 1, }}>
<Button
title="select text"
onPress={() => {
TextInputState.focusTextInput(findNodeHandle(this.inputRef))
}}
</
<TextInput
selectTextOnFocus
ref={ref => this.inputRef = ref}
/>
</View>
);
}
}https://stackoverflow.com/questions/44718405
复制相似问题