我为您准备了一些解决方法。一旦你开始输入,下划线就会被删除。
演示
说明
我将borderBottomWidth 和borderBottomColor 应用于父视图,因为您可能不想使用multiline。如果您想使用多文本输入,您可以将这些样式直接应用于文本输入,如 docs 中所述。
请注意,某些道具仅适用于 multiline={true/false}。
此外,仅适用于元素一侧的边框样式
(例如,borderBottomColor、borderLeftWidth 等)将不会被应用
如果多行=假。为了达到同样的效果,你可以将你的
视图中的文本输入
一旦用户输入(文本长度大于 0)某些内容,this.setState({ active: true }) 就会被触发。之后在此处进行条件渲染:
borderBottomColor: this.state.active === false ? 'red' : 'transparent' // hide color, when text is present
width: this.state.active === false ? scaledWidth : 100 // increase width to 100. Value is just a placeholder, use whatever you like
完整代码
// helper function to scale font size and placeholder width.
scale = (fontSize) => {
const screenWidth = Dimensions.get('window').width;
const ratio = fontSize / 375; // get ratio based on your standard scale, here: width of iphone 7
const newSize = Math.round(ratio * screenWidth);
return newSize;
}
render() {
// you probably have to play around with those standard values
const scaledFontSize = this.scale(22);
const scaledWidth = this.scale(25);
return (
<View style={{ marginTop: 50, flex: 1, alignItems: 'flex-end' }}>
<View style={{ borderBottomWidth: 2, borderBottomColor: this.state.active === false ? 'red' : 'transparent', width: this.state.active === false ? scaledWidth : 100 }}>
<TextInput
style={{ height: 30, textAlign: 'right', fontSize: scaledFontSize }}
onChangeText={(text) => text.length > 0 ? this.setState({ active: true }) : this.setState({ active: false })}
placeholder={'10'}
//multiline
/>
</View>
</View>
)
}