如何更改 React Native 应用程序中文本输入的边框颜色

问题描述 投票:0回答:2

当文本输入字段聚焦时,如何更改边框颜色或如何在本机反应中添加或更改文本输入字段中的样式。 (适用于安卓)

react-native
2个回答
36
投票

您可以使用

onFocus
onBlur
来完成这项工作

state: {
    isFocused: true
}

 handleFocus = () => this.setState({isFocused: true})

 handleBlur = () => this.setState({isFocused: false})

 <TextInput
         onFocus={this.handleFocus}
         onBlur={this.handleBlur}
         style={[//Your Styles, {
             borderBottomColor: this.state.isFocused
                 ? 'black'
                 : 'red',
             borderBottomWidth: 1,
         }]}
     />

3
投票

使用以下代码有助于更改 React Native 应用程序的边框颜色。

export default class HomeActivity extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.headerText}>
          Remove TextInput Component Bottom Underline in React Native
        </Text>

        <TextInput
          style={{
            height: 40,
            width: "95%",
            borderColor: "gray",
            borderWidth: 1,
            marginBottom: 20,
          }}
          // Adding hint in TextInput using Placeholder option.
          placeholder="Enter Your First Name"
          // Making the Under line Transparent.
          underlineColorAndroid="transparent"
        />

        <TextInput
          style={{
            height: 40,
            width: "95%",
            borderColor: "red",
            borderWidth: 1,
            marginBottom: 20,
          }}
          // Adding hint in TextInput using Placeholder option.
          placeholder="Enter Your First Name"
          // Making the Under line Transparent.
          underlineColorAndroid="transparent"
        />

        <TextInput
          style={{
            height: 40,
            width: "95%",
            borderColor: "blue",
            borderWidth: 1,
            marginBottom: 20,
          }}
          // Adding hint in TextInput using Placeholder option.
          placeholder="Enter Your First Name"
          // Making the Under line Transparent.
          underlineColorAndroid="transparent"
        />
      </View>
    );
  }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.