gpt4 book ai didi

javascript - 如何在 React Native 中添加按钮?

转载 作者:IT王子 更新时间:2023-10-29 03:06:04 26 4
gpt4 key购买 nike

我对这整个“无 CSS”的事情感到困惑,但我明白为什么它是有益的。我只想在屏幕中间放置一个按钮,但我还不明白 React 中的样式是如何工作的。这是我的代码:

var tapSpeed = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Tap me as fast as you can!
</Text>
<View style={styles.button}>
!
</View>
</View>
);
}
});

var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFCCCC'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
button: {
textAlign: 'center',
color: '#ffffff',
marginBottom: 7,
border: 1px solid blue,
borderRadius: 2px
}
});

最佳答案

更新:使用内置 Button component .

弃用:

将您的 View 包装到 TouchableHighlight 中适用于 iOS 和 TouchableNativeFeedback适用于 Android。

var {
Platform,
TouchableHighlight,
TouchableNativeFeedback
} = React;

var tapSpeed = React.createClass({
buttonClicked: function() {
console.log('button clicked');
},
render: function() {
var TouchableElement = TouchableHighlight;
if (Platform.OS === 'android') {
TouchableElement = TouchableNativeFeedback;
}
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Tap me as fast as you can!
</Text>
<TouchableElement
style={styles.button}
onPress={this.buttonClicked.bind(this)}>
<View>
<Text style={styles.buttonText}>Button!</Text>
</View>
</TouchableElement>
</View>
);
}
});

关于javascript - 如何在 React Native 中添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29872918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com