如何在React Native中添加按钮?

bar*_*eek 43 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
  }
});
Run Code Online (Sandbox Code Playgroud)

Yev*_*nov 34

更新:使用内置的Button组件.

推荐使用:

包装你查看到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>
    );
  }
});       
Run Code Online (Sandbox Code Playgroud)


Ash*_*k R 26

您可以使用内置的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);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在这里阅读更多.


ide*_*ide 25

反应天然按钮包提供的样式等天然按钮的按钮.安装它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!');
  },
});
Run Code Online (Sandbox Code Playgroud)

  • 您应该添加`--save`,如下所示:`npm install react-native-button --save`. (3认同)
  • 这看起来在iOS上是原生的,而不是在Android上. (3认同)
  • 不工作......出现此错误:错误:需要未知模块"react-native-button".但我在使用之前已经安装了包 (2认同)
  • @ backslash112尝试重新启动打包程序. (2认同)