TextInput
TextInput 是一个允许用户在应用中通过键盘输入文本的基本组件。本组件的属性提供了多种特性的配置,譬如自动完成、自动大小写、占位文字,以及多种不同的键盘类型(如纯数字键盘)等等。
最简单的用法就是丢一个TextInput
到应用里,然后订阅它的onChangeText
事件来读取用户的输入。注意,从 TextInput 里取值这就是目前唯一的做法!也就是使用在onChangeText
中用setState
把用户的输入写入到 state 中,然后在需要取值的地方从 this.state 中取出值。它还有一些其它的事件,譬如onSubmitEditing
和onFocus
。一个简单的例子如下:
Two methods exposed via the native element are .focus() and .blur() that will focus or blur the TextInput programmatically.
注意有些属性仅在multiline
为 true 或者为 false 的时候有效。此外,当multiline=false
时,为元素的某一个边添加边框样式(例如:borderBottomColor
,borderLeftWidth
等)将不会生效。为了能够实现效果你可以使用一个View
来包裹TextInput
:
TextInput
has a border at the bottom of its view by default. This border has its padding set by the background image provided by the system, and it cannot be changed. Solutions to avoid this are to either not set height explicitly, in which case the system will take care of displaying the border in the correct position, or to not display the border by setting underlineColorAndroid
to transparent.
又又,在安卓上长按选择文本会导致windowSoftInputMode
设置变为adjustResize
,这样可能导致绝对定位的元素被键盘给顶起来。要解决这一问题你需要在 AndroidManifest.xml 中明确指定合适的windowSoftInputMode
(https://developer.android.com/guide/topics/manifest/activity-element.html)值,或是自己监听事件来处理布局变化。
Reference
Props
View Props
Inherits View Props.
allowFontScaling
控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。默认值为true
。
Type |
---|
bool |
autoCapitalize
控制 TextInput 是否要自动将特定字符切换为大写,This property is not supported by some keyboard types such as name-phone-pad
.
characters
: 所有的字符。words
: 每个单词的第一个字符。sentences
: 每句话的第一个字符(默认)。none
: 不切换。
Type |
---|
enum('none', 'sentences', 'words', 'characters') |
autoComplete
Specifies autocomplete hints for the system, so it can provide autofill. On Android, the system will always attempt to offer autofill by using heuristics to identify the type of content. To disable autocomplete, set autoComplete
to off
.
The following values work across platforms:
additional-name
address-line1
address-line2
birthdate-day
(iOS 17+)birthdate-full
(iOS 17+)birthdate-month
(iOS 17+)birthdate-year
(iOS 17+)cc-csc
(iOS 17+)cc-exp
(iOS 17+)cc-exp-day
(iOS 17+)cc-exp-month
(iOS 17+)cc-exp-year
(iOS 17+)cc-number
country
current-password
email
family-name
given-name
honorific-prefix
honorific-suffix
name
new-password
off
one-time-code
postal-code
street-address
tel
username
The following values work on iOS only:
cc-family-name
(iOS 17+)cc-given-name
(iOS 17+)cc-middle-name
(iOS 17+)cc-name
(iOS 17+)cc-type
(iOS 17+)nickname
organization
organization-title
url
The following values work on Android only:
gender
name-family
name-given
name-middle
name-middle-initial
name-prefix
name-suffix
password
password-new
postal-address
postal-address-country
postal-address-extended
postal-address-extended-postal-code
postal-address-locality
postal-address-region
sms-otp
tel-country-code
tel-device
tel-national
username-new
Type |
---|
enum('additional-name', 'address-line1', 'address-line2', 'birthdate-day', 'birthdate-full', 'birthdate-month', 'birthdate-year', 'cc-csc', 'cc-exp', 'cc-exp-day', 'cc-exp-month', 'cc-exp-year', 'cc-number', 'country', 'current-password', 'email', 'family-name', 'given-name', 'honorific-prefix', 'honorific-suffix', 'name', 'new-password', 'off', 'one-time-code', 'postal-code', 'street-address', 'tel', 'username', 'cc-family-name', 'cc-given-name', 'cc-middle-name', 'cc-name', 'cc-type', 'nickname', 'organization', 'organization-title', 'url', 'gender', 'name-family', 'name-given', 'name-middle', 'name-middle-initial', 'name-prefix', 'name-suffix', 'password', 'password-new', 'postal-address', 'postal-address-country', 'postal-address-extended', 'postal-address-extended-postal-code', 'postal-address-locality', 'postal-address-region', 'sms-otp', 'tel-country-code', 'tel-device', 'tel-national', 'username-new') |
autoCorrect
如果为 false,会关闭拼写自动修正。默认值是 true。
Type |
---|
bool |
autoFocus
如果为 true,在componentDidMount
后会获得焦点。默认值为 false。
Type |
---|
bool |
blurOnSubmit
如果为 true,文本框会在提交的时候失焦。对于单行输入框默认值为 true,多行则为 false。注意:对于多行输入框来说,如果将blurOnSubmit
设为 true,则在按下回车键时就会失去焦点同时触发onSubmitEditing
事件,而不会换行。
Type |
---|
bool |
caretHidden
如果为 true,则隐藏光标。默认值为 false。
Type |
---|
bool |