我只想创建一个简单的TicTacToe游戏。为此,我需要9个按钮。我使用被TouchableHighlights包围的视图创建了基本布局,以使其可单击。
如果我单击“按钮”,则仅该按钮应更改其颜色,但是当前所有按钮均会同时更改颜色。
有人知道如何解决此问题吗?
我不知道如何为每个TouchableHighlight分别处理状态。
真的感谢所有帮助。
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Dimensions,
TouchableHighlight
} from 'react-native';
var screenWidth = Dimensions.get('window').width;
screenWidth-=36;
var screenHeigth = Dimensions.get('window').height;
let randomHex = () => {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
export default class randomBackground extends Component {
constructor(props) {
super(props)
this.state = {
backgroundColor: '#f5aef5'
};
}
render() {
return (
<View style={{backgroundColor: '#f5f5f5', flex: 1}}>
<Text style={{color:'black', alignSelf: 'center', fontSize: 100, marginTop: 60}}>TicTacToe</Text>
<View style={styles.outercardview}>
{/* row 1 */}
<View style={{flexDirection:'row'}}>
<TouchableHighlight onPress={() => this.setState({backgroundColor: randomHex()})} underlayColor="white" style={{ height: screenWidth/3, width: screenWidth/3, borderRadius: 9, margin: 3, backgroundColor: this.state.backgroundColor}}>
<View>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={() =>this.setState({backgroundColor: randomHex()})} underlayColor="white" style={{ height: screenWidth/3, width: screenWidth/3, borderRadius: 9, margin: 3, backgroundColor: this.state.backgroundColor}}>
<View>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={() => this.setState({backgroundColor: randomHex()})} underlayColor="white" style={{ height: screenWidth/3, width: screenWidth/3, borderRadius: 9, margin: 3, backgroundColor: this.state.backgroundColor}}>
<View>
</View>
</TouchableHighlight>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
outercardview: {
marginTop: (screenHeigth/2)-(screenWidth/2)-140,
marginBottom: 20,
marginRight: 10,
marginLeft: 10,
justifyContent: 'center',
height:screenWidth,
shadowOffset: {
width: 2,
height: 2,
},
shadowColor: 'grey',
shadowOpacity: 0.1,
}
});
AppRegistry.registerComponent('randomBackground', () => randomBackground);
最佳答案
尝试这个:
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Dimensions,
TouchableHighlight
} from 'react-native';
var screenWidth = Dimensions.get('window').width;
screenWidth-=36;
var screenHeigth = Dimensions.get('window').height;
let randomHex = () => {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
export default class randomBackground extends Component {
constructor(props) {
super(props)
this.state = {
backgroundColor1: '#f5aef5',
backgroundColor2: '#f5aef5',
backgroundColor3: '#f5aef5'
};
}
render() {
return (
<View style={{backgroundColor: '#f5f5f5', flex: 1}}>
<Text style={{color:'black', alignSelf: 'center', fontSize: 100, marginTop: 60}}>TicTacToe</Text>
<View style={styles.outercardview}>
{/* row 1 */}
<View style={{flexDirection:'row'}}>
<TouchableHighlight onPress={() => this.setState({backgroundColor1: randomHex()})} underlayColor="white" style={{ height: screenWidth/3, width: screenWidth/3, borderRadius: 9, margin: 3, backgroundColor: this.state.backgroundColor1}}>
<View>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={() =>this.setState({backgroundColor2: randomHex()})} underlayColor="white" style={{ height: screenWidth/3, width: screenWidth/3, borderRadius: 9, margin: 3, backgroundColor: this.state.backgroundColor2}}>
<View>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={() => this.setState({backgroundColor3: randomHex()})} underlayColor="white" style={{ height: screenWidth/3, width: screenWidth/3, borderRadius: 9, margin: 3, backgroundColor: this.state.backgroundColor3}}>
<View>
</View>
</TouchableHighlight>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
outercardview: {
marginTop: (screenHeigth/2)-(screenWidth/2)-140,
marginBottom: 20,
marginRight: 10,
marginLeft: 10,
justifyContent: 'center',
height:screenWidth,
shadowOffset: {
width: 2,
height: 2,
},
shadowColor: 'grey',
shadowOpacity: 0.1,
}
});
AppRegistry.registerComponent('randomBackground', () => randomBackground);