Javascript 在 React Native 中按下时更改按钮样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34625829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:39:08  来源:igfitidea点击:

Change button style on press in React Native

javascriptreactjsreact-native

提问by domi91c

I'd like the style of a button in my app to change when it is being pressed. What is the best way to do this?

我希望我的应用程序中按钮的样式在按下时发生变化。做这个的最好方式是什么?

回答by Besart Hoxhaj

Use TouchableHighlight.

使用TouchableHighlight.

Here an example:

这里有一个例子:

enter image description here

在此处输入图片说明

'use strict';

 import React, {
Component,
StyleSheet,
PropTypes,
View,
Text,
TouchableHighlight
} from "react-native";

export default class Home extends Component {
constructor(props) {
    super(props);
    this.state = { pressStatus: false };
}
_onHideUnderlay() {
    this.setState({ pressStatus: false });
}
_onShowUnderlay() {
    this.setState({ pressStatus: true });
}
render() {
    return (
        <View style={styles.container}>
            <TouchableHighlight
                activeOpacity={1}
                style={
                    this.state.pressStatus
                        ? styles.buttonPress
                        : styles.button
                }
                onHideUnderlay={this._onHideUnderlay.bind(this)}
                onShowUnderlay={this._onShowUnderlay.bind(this)}
            >
                <Text
                    style={
                        this.state.pressStatus
                            ? styles.welcomePress
                            : styles.welcome
                    }
                >
                    {this.props.text}
                </Text>
            </TouchableHighlight>
        </View>
    );
}
}

Home.propTypes = {
text: PropTypes.string.isRequired
};

const styles = StyleSheet.create({
container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#F5FCFF"
},
welcome: {
    fontSize: 20,
    textAlign: "center",
    margin: 10,
    color: "#000066"
},
welcomePress: {
    fontSize: 20,
    textAlign: "center",
    margin: 10,
    color: "#ffffff"
},
button: {
    borderColor: "#000066",
    borderWidth: 1,
    borderRadius: 10
},
buttonPress: {
    borderColor: "#000066",
    backgroundColor: "#000066",
    borderWidth: 1,
    borderRadius: 10
}
});

回答by Giovanny Gonzalez

Use the prop:

使用道具:

underlayColor

<TouchableHighlight style={styles.btn} underlayColor={'gray'} />

https://facebook.github.io/react-native/docs/touchablehighlight.html

https://facebook.github.io/react-native/docs/touchablehighlight.html

回答by sami ghazouane

use something like that :

使用类似的东西:

class A extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      onClicked: false
    }
    this.handlerButtonOnClick = this.handlerButtonOnClick.bind(this);
  }
  handlerButtonOnClick(){
    this.setState({
       onClicked: true
    });
  }
  render() {
    var _style;
    if (this.state.onClicked){ // clicked button style
      _style = {
          color: "red"
        }
    }
    else{ // default button style
      _style = {
          color: "blue"
        }
    }
    return (
        <div>
            <button
                onClick={this.handlerButtonOnClick}
                style={_style}>Press me !</button>
        </div>
    );
  }
}

If you use an external CSS, you can use className in place of style property :

如果您使用外部 CSS,您可以使用 className 代替 style 属性:

render() {
    var _class = "button";
    var _class.concat(this.state.onClicked ? "-pressed" : "-normal") ;
    return (
        <div>
            <button
                onClick={this.handlerButtonOnClick}
                className={_class}>Press me !</button>
        </div>
    );
  }

It doesn't really matter how do you apply your CSS. Keep your eyes on the "handlerButtonOnClick" method.

如何应用 CSS 并不重要。密切关注“handlerButtonOnClick”方法。

When the state change, the component is re-rendered ("render" method is called again).

当状态改变时,组件被重新渲染(再次调用“render”方法)。

Good luck ;)

祝你好运 ;)

回答by Bruce Lee

This is Besart Hoxhaj's answer in ES6. When i answer this, React Native is 0.34.

这是ES6 中Besart Hoxhaj的回答。当我回答这个问题时,React Native 是 0.34。

 import React from "react";
 import { TouchableHighlight, Text, Alert, StyleSheet } from "react-native";

 export default class TouchableButton extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        pressed: false
    };
}
render() {
    return (
        <TouchableHighlight
            onPress={() => {
                // Alert.alert(
                //     `You clicked this button`,
                //     'Hello World!',
                //     [
                //         {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
                //         {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
                //         {text: 'OK', onPress: () => console.log('OK Pressed')},
                //     ]
                // )
            }}
            style={[
                styles.button,
                this.state.pressed ? { backgroundColor: "green" } : {}
            ]}
            onHideUnderlay={() => {
                this.setState({ pressed: false });
            }}
            onShowUnderlay={() => {
                this.setState({ pressed: true });
            }}
        >
            <Text>Button</Text>
        </TouchableHighlight>
    );
}
}

const styles = StyleSheet.create({
button: {
    padding: 10,
    borderColor: "blue",
    borderWidth: 1,
    borderRadius: 5
}
});