我使用的是TouchableHighlight,但我只能使用underlayColor更改背景色.但如何改变其他内容呢?

推荐答案

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight
} = React;

var  colors = ['#ddd', '#efefef', 'red', '#666', 'rgba(0,0,0,.1)', '#ededed'];
var backgroundcolors = ['green', 'black', 'orange', 'blue', 'purple', 'pink'];

var SampleApp = React.createClass({

  getInitialState() {
    return {
        color: 'orange',
      backgroundColor: 'rgba(0,0,0,.1)'
    }
  },

  _changeStyle() {
    var color = colors[Math.floor(Math.random()*colors.length)];
    var backgroundColor = backgroundcolors[Math.floor(Math.random()*backgroundcolors.length)];
    this.setState({
        color: color,
      backgroundColor: backgroundColor
    })
  },

  render: function() {
    return (
      <View style={styles.container}>
        <TouchableHighlight 
        onPress={ () => this._changeStyle() }
        style={{ backgroundColor: this.state.backgroundColor, height: 60, flexDirection: 'row', alignItems:'center', justifyContent: 'center' }}>
                <Text style={{ fontSize: 22, color: this.state.color }}>CHANGE COLOR</Text>
        </TouchableHighlight>

        <TouchableHighlight style={{ backgroundColor: 'red', height: 60, flexDirection: 'row', alignItems:'center', justifyContent: 'center' }}>
          <Text style={{ color: 'white', fontSize:22 }} >Click Me</Text>
        </TouchableHighlight>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop:60
  }
});

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

React-native相关问答推荐

Reaction-原生NavBottom设计背景

嵌套的FlatList内的Incorrect Padding

GetStream:更改消息气泡背景

错误:图片:找不到 ID 为1的assets资源 .请判断图像源或打包器

如何防止 VS Code 从react-native-web自动导入?

React Native - 如何在从另一个屏幕导航后将 ScrollView 滚动到给定位置

react-redux connect 和 redux 数据的组件生命周期顺序

React Navigation v5 中的初始路由参数?

React-native 重置数据库

React Native Child Parent 通信

使用 resizeMode 封面截断的图像

'React native run android' 在模拟器中启动应用程序后立即停止

如何更改 React Native(android)应用程序的远程推送通知图标

Firebase JavaScript SDK 和 react-native-firebase 有什么区别

React Native 检测点击 View

如何将props传递给 React Navigation 导航器中的组件?

使用 Jest 的 react-navigation 测试组件

动态改变 React Native Flat List 中的列数

在 `../node_modules/react-native/React` 中找不到 `React-Core` 的 podspec

移动应用程序不需要 CORS?