如何在React Native中添加按钮?

时间:2015-04-26 02:15:17

标签: javascript reactjs react-native

我对这整个“没有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
  }
});

9 个答案:

答案 0 :(得分:34)

更新:使用内置Button component

<强>推荐使用:

将视图包含在适用于iOS的TouchableHighlight和适用于Android的TouchableNativeFeedback

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>
    );
  }
});       

答案 1 :(得分:25)

您可以使用内置的react-native Button元素。

import React, { Component } from 'react';
import { StyleSheet, View, Button, Alert, AppRegistry } from 'react-native';

class MainApp extends Component {

_onPress() {
  Alert.alert('on Press!');
 }

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.buttonContainer}>
          <Button onPress={this._onPress} title="Hello" color="#FFFFFF" accessibilityLabel="Tap on Me"/>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFFFFF'
  },
  buttonContainer: {
    backgroundColor: '#2E9298',
    borderRadius: 10,
    padding: 10,
    shadowColor: '#000000',
    shadowOffset: {
      width: 0,
      height: 3
    },
    shadowRadius: 10,
    shadowOpacity: 0.25
  }
})

AppRegistry.registerComponent('MainApp', () => MainApp);

enter image description here

阅读更多Here

答案 2 :(得分:24)

react-native-button包提供了一个类似于原生按钮的按钮。使用npm install react-native-button安装它并在组件中使用它,如下所示:

var Button = require('react-native-button');

var ExampleComponent = React.createClass({
  render() {
    return (
      <Button
        style={{borderWidth: 1, borderColor: 'blue'}}
        onPress={this._handlePress}>
        Press Me!
      </Button>
    );
  },

  _handlePress(event) {
    console.log('Pressed!');
  },
});

答案 3 :(得分:1)

您有两个选项可以实现可触摸的组件/按钮来处理用户的事件。

关于本机反应样式,您需要了解flexbox布局。检查此css flexbox文章所有规则都适用于本地反应https://css-tricks.com/snippets/css/a-guide-to-flexbox/,但您必须将规则(例如align-content)大写为alignContent

答案 4 :(得分:1)

<Button
  onPress={onPressLearnMore}
  title="Learn More"
  color="#841584"
  accessibilityLabel="Learn more about this purple button"
/>

答案 5 :(得分:0)

请检查react-native doc's有关按钮的信息

您可以通过多种方式在应用程序中添加按钮并设置样式

你可以使用Button标签,它只有一种颜色属性的样式,它会出现在IOS中不同于Android,或者通过将按钮放在带有样式的视图标签中

sherpa is not a palindrome stewie is not a palindrome anna is a palindrome lil squiggle is not a palindrome racecar is a palindrome tacocat is a palindrome

并检查TouchableOpacity和TouchableNativeFeedback标签

并锁定以下链接,以获取更多选项,以便在您的应用中添加自定义按钮

https://js.coach/react-native/react-native-action-button?search=button

答案 6 :(得分:0)

export default class Login extends React.Component {
  barcodeAction = () => {
    this.props.navigation.navigate('BarCodeScanner')
  }

  cleverTapAction = () => {
    this.props.navigation.navigate('CleverTapApp')
  }
} 

render() {
    return (
      <View style={styles.container}>
        <View style={styles.buttonContainer}>
          <Button
            onPress={this._onPressButton}
            title="Press Me"
          />
        </View>
        <View style={styles.buttonContainer}>
          <Button
            onPress={this._onPressButton}
            title="Press Me"
            color="#841584"
          />
        </View>
        <View style={styles.alternativeLayoutButtonContainer}>
          <Button
            onPress={this._onPressButton}
            title="This looks great!"
          />
          <Button
            onPress={this._onPressButton}
            title="OK!"
            color="#841584"
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
   flex: 1,
   justifyContent: 'center',
  },
  buttonContainer: {
    margin: 20
  },
  alternativeLayoutButtonContainer: {
    margin: 20,
    flexDirection: 'row',
    justifyContent: 'space-between'
  }
});

enter image description here

答案 7 :(得分:0)

Button包中的react-native元素不提供内置样式功能。例如默认情况下,“ title”道具为大写。因此,我使用了另一个软件包react-native-elements,该软件包为Button元素提供了不错的功能以及不同的样式选项。

您可以在Button from react-native-elements

上引用更多详细信息

答案 8 :(得分:0)

import React, { Component } from 'react';
import { StyleSheet, View, TouchableOpacity, Text} from 'react-native';
var tapSpeed = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <TouchableOpacity>
          <Text style={styles.welcome}>
            Tap me as fast as you can!
          </Text>
        </TouchableOpacity>
        <TouchableOpacity style={styles.button}>
          <Text>!</Text>
        </TouchableOpacity>
      </View>
    );
  }
});

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