#スタイル用コード
style.js
import { StyleSheet } from "react-native";
const styles = StyleSheet.create({
container: {
backgroundColor: '#FFF',
},
countArea: {
marginTop: 140,
justifyContent: "center",
alignItems: "center",
marginBottom:10
},
buttonArea: {
marginTop: 5,
justifyContent: 'center',
alignItems: 'center'
},
value: {
fontSize: 30
},
topbutton: {
marginTop: 50
},
button: {
marginTop: 5,
justifyContent: 'center',
alignItems: 'center',
width: 50,
height: 40,
borderRadius: 5,
borderWidth: 1,
borderColor: '#fff'
},
buttonText: {
fontSize: 25,
color: '#fff'
},
buttonUp: {
backgroundColor: '#A53434',
fontWeight: "bold"
},
buttonUpText: {
backgroundColor: 'red'
},
buttonMinus: {
backgroundColor: '#343EA5'
},
buttonTimes: {
backgroundColor: 'purple'
},
buttonDivide: {
backgroundColor: "green"
},
buttonReset: {
borderColor: '#AFADAD',
marginBottom: 200
},
buttonResetText: {
color: '#000',
fontSize:11
},
buttonNum: {
backgroundColor: 'grey',
fontWeight: "bold",
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
width: 60,
height: 40,
borderRadius: 5,
borderWidth: 1,
borderColor: '#fff'
},
buttonEqual: {
backgroundColor: "black"
}
});
export default (styles);
#レイアウト完了後のコード
App.js
import React, {Component} from 'react';
import styles from "./style"
import {TouchableOpacity, Text, View} from 'react-native';
export default class App extends Component {
render() {
return (
<View>
<View style={styles.countArea}>
<Text style={styles.value}>0 ? 0 = 0</Text>
</View>
<View style={styles.buttonArea}>
<View style={{flexDirection: 'row'}}>
{array1.map(i => (
<TouchableOpacity style={styles.topbutton,styles.buttonNum} key={i}>
<Text style={styles.buttonText}> {i} </Text>
</TouchableOpacity>
))}
</View>
<View style={{ flexDirection: 'row'}}>
{array2.map(i => (
<TouchableOpacity style={styles.topbutton, styles.buttonNum} key={i} >
<Text style={styles.buttonText}> {i} </Text>
</TouchableOpacity>
))}
</View>
<View style={{ flexDirection: 'row'}}>
<TouchableOpacity style={[styles.button, styles.buttonUpText]}>
<Text style={styles.buttonText}>+</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonMinus]} >
<Text style={styles.buttonText}>-</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonTimes]} >
<Text style={styles.buttonText}>x</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonDivide]} >
<Text style={styles.buttonText}>÷</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonEqual]} >
<Text style={styles.buttonText}>=</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonReset]} >
<Text style={[styles.buttonResetText]}>リセット</Text>
</TouchableOpacity>
</View>
</View>
</View>
);
};
};
const array1 = [];
for (let i = 0; i< 5; i++) {
array1[i] = i
}
const array2 = [];
for (let i = 5; i< 10; i++) {
array2[i] = i
}
#左右の数字と四則演算の入力ができるコード
App.js
import React, {Component} from 'react';
import styles from "./style"
import {TouchableOpacity, Text, View} from 'react-native';
export default class App extends Component {
constructor(props){
super(props);
this.state = {
leftNum: 0,
mark: "?",
rightNum: 0,
}
}
onNum(input){
if (this.state.mark == "?"){
let num = Number(String(this.state.leftNum) + String(input["i"]))
this.setState({ leftNum: num })
} else {
let num = Number(String(this.state.rightNum) + String(input["i"]))
this.setState({ rightNum: num })
}
}
onMark(input){
if (input == "plus"){
this.setState({ mark: "+" })
} else if (input == "minus") {
this.setState({ mark: "-" })
} else if (input == "times") {
this.setState( { mark: "x"} )
} else if (input == "divide") {
this.setState({ mark: "➗"})
}
}
render() {
return (
<View>
<View style={styles.countArea}>
<Text style={styles.value}>{this.state.leftNum} {this.state.mark} {this.state.rightNum} = 0</Text>
</View>
<View style={styles.buttonArea}>
<View style={{flexDirection: 'row'}}>
{array1.map(i => (
<TouchableOpacity style={styles.topbutton,styles.buttonNum} key={i} onPress={() => this.onNum({i})}>
<Text style={styles.buttonText}> {i} </Text>
</TouchableOpacity>
))}
</View>
<View style={{ flexDirection: 'row'}}>
{array2.map(i => (
<TouchableOpacity style={styles.topbutton, styles.buttonNum} key={i} onPress={() => this.onNum({i})} >
<Text style={styles.buttonText}> {i} </Text>
</TouchableOpacity>
))}
</View>
<View style={{ flexDirection: 'row'}}>
<TouchableOpacity style={[styles.button, styles.buttonUpText]} onPress={() => this.onMark("plus")}>
<Text style={styles.buttonText}> + </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonMinus]} onPress={() => this.onMark("minus")}>
<Text style={styles.buttonText}> - </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonTimes]} onPress={() => this.onMark("times")}>
<Text style={styles.buttonText}> x </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonDivide]} onPress={() => this.onMark("divide")}>
<Text style={styles.buttonText}> ÷ </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonEqual]} >
<Text style={styles.buttonText}> = </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonReset]} >
<Text style={[styles.buttonResetText]}>リセット</Text>
</TouchableOpacity>
</View>
</View>
</View>
);
};
};
const array1 = [];
for (let i = 0; i< 5; i++) {
array1[i] = i
}
const array2 = [];
for (let i = 5; i< 10; i++) {
array2[i] = i
}
#完成版コード
App.js
import React, {Component} from 'react';
import styles from "./style"
import {TouchableOpacity, Text, View} from 'react-native';
export default class App extends Component {
constructor(props){
super(props);
this.state = {
leftNum: 0,
mark: "?",
rightNum: 0,
result: 0
}
}
onNum(input){
if (this.state.mark == "?"){
let num = Number(String(this.state.leftNum) + String(input["i"]))
this.setState({ leftNum: num })
} else {
let num = Number(String(this.state.rightNum) + String(input["i"]))
this.setState({ rightNum: num })
}
}
onMark(input){
if (input == "plus"){
this.setState({ mark: "+" })
} else if (input == "minus") {
this.setState({ mark: "-" })
} else if (input == "times") {
this.setState( { mark: "x"} )
} else if (input == "divide") {
this.setState({ mark: "➗"})
}
}
onEqual(){
if (this.state.mark == "+") {
this.setState( {result: this.state.leftNum+ this.state.rightNum })
} else if (this.state.mark == "-") {
this.setState( { result: this.state.leftNum- this.state.rightNum})
} else if (this.state.mark == "x") {
this.setState( { result: this.state.leftNum* this.state.rightNum})
} else if (this.state.mark == "➗") {
this.setState( { result: this.state.leftNum/ this.state.rightNum})
}
}
onReset(){
this.setState({ leftNum: 0, mark: "?", rightNum:0, result:0 })
}
render() {
return (
<View>
<View style={styles.countArea}>
<Text style={styles.value}>{this.state.leftNum} {this.state.mark} {this.state.rightNum} = {this.state.result}</Text>
</View>
<View style={styles.buttonArea}>
<View style={{flexDirection: 'row'}}>
{array1.map(i => (
<TouchableOpacity style={styles.topbutton,styles.buttonNum} key={i} onPress={() => this.onNum({i})}>
<Text style={styles.buttonText}> {i} </Text>
</TouchableOpacity>
))}
</View>
<View style={{ flexDirection: 'row'}}>
{array2.map(i => (
<TouchableOpacity style={styles.topbutton, styles.buttonNum} key={i} onPress={() => this.onNum({i})} >
<Text style={styles.buttonText}> {i} </Text>
</TouchableOpacity>
))}
</View>
<View style={{ flexDirection: 'row'}}>
<TouchableOpacity style={[styles.button, styles.buttonUpText]} onPress={() => this.onMark("plus")}>
<Text style={styles.buttonText}> + </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonMinus]} onPress={() => this.onMark("minus")}>
<Text style={styles.buttonText}> - </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonTimes]} onPress={() => this.onMark("times")}>
<Text style={styles.buttonText}> x </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonDivide]} onPress={() => this.onMark("divide")}>
<Text style={styles.buttonText}> ÷ </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonEqual]} onPress={() => this.onEqual()} >
<Text style={styles.buttonText}> = </Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.buttonReset]} onPress={() => this.onReset()}>
<Text style={[styles.buttonResetText]}>リセット</Text>
</TouchableOpacity>
</View>
</View>
</View>
);
};
};
const array1 = [];
for (let i = 0; i< 5; i++) {
array1[i] = i
}
const array2 = [];
for (let i = 5; i< 10; i++) {
array2[i] = i
}