react-native 写一个布局合理的计算器,这么难吗?

import React, { useState } from 'react';

import { View, Text, StyleSheet, Button, TouchableHighlight   } from 'react-native';

const CalculatorApp = () => {

  const [input, setInput] = useState('');

  const [result, setResult] = useState('');

  const handleInput = (value: any) => {

    setInput(input + value);

  };

  const calculateResult = () => {

    try {

      setResult(eval(input).toString());

    } catch (error) {

      setResult('Error');

    }

  };

  const clearInput = () => {

    setInput('');

    setResult('');

  };

const styles = StyleSheet.create({

  container: {

    flex: 1,

    justifyContent: 'center',

    alignItems: 'center'

  },

  buttonRow: {

   

    flexDirection: 'row',

    justifyContent: 'space-between',

    alignItems: 'center',

    marginBottom: 20,

    width: '100%'

  },

  input: {

    flex:1,

    fontSize: 24,

    marginBottom: 10

  },

  result: {

    flex:1,

    fontSize: 30,

    marginBottom: 20

  },

  buttonWrapper: {

    flex: 1,

    justifyContent: 'center',

    alignItems: 'center',

  },

  buttonText: {

    fontSize: 18, // 或您需要的其他大小

    color: 'black', // 文本颜色

  },

  // 修改后的 button22 样式

  button22: {

    flex: 1,

    justifyContent: 'center',

    alignItems: 'center',

    padding: 10,

    minHeight: 40, // 或您需要的高度

  },

  buttonContainer: {

    flex:3,

    flexDirection: 'column',

    flexWrap: 'wrap',

    justifyContent: 'space-between'

  },

  // 可以根据需要添加更多样式

});

 // 接下来添加样式和 UI

return (

  <View style={styles.container}>

    <Text style={styles.input}>{input}</Text>

    <Text style={styles.result}>{result}</Text>

    <View style={styles.buttonContainer}>

   

    <View style={styles.buttonRow}>

   

      <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('1')}>

            <Text style={styles.buttonText}>1</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('2')}>

            <Text style={styles.buttonText}>2</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('3')}>

            <Text style={styles.buttonText}>3</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('4')}>

            <Text style={styles.buttonText}>4</Text>

          </TouchableHighlight>

        </View>

       

    </View>

    <View style={styles.buttonRow}>

    <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('5')}>

            <Text style={styles.buttonText}>5</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('7')}>

            <Text style={styles.buttonText}>6</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('7')}>

            <Text style={styles.buttonText}>7</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('8')}>

            <Text style={styles.buttonText}>8</Text>

          </TouchableHighlight>

        </View>

   

    </View>

    <View style={styles.buttonRow}>

    <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('9')}>

            <Text style={styles.buttonText}>9</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('0')}>

            <Text style={styles.buttonText}>0</Text>

          </TouchableHighlight>

        </View>

    </View>

    <View style={styles.buttonRow}>

    <View style={styles.buttonWrapper}>

    <TouchableHighlight style={styles.button22} onPress={() => handleInput('+')}>

            <Text style={styles.buttonText}>+</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('-')}>

            <Text style={styles.buttonText}>-</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('*')}>

            <Text style={styles.buttonText}>*</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => handleInput('/')}>

            <Text style={styles.buttonText}>/</Text>

          </TouchableHighlight>

        </View>

   

    </View>

    <View style={styles.buttonRow}>

    <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => clearInput()}>

            <Text style={styles.buttonText}>cleare</Text>

          </TouchableHighlight>

        </View>

        <View style={styles.buttonWrapper}>

          <TouchableHighlight style={styles.button22} onPress={() => calculateResult()}>

            <Text style={styles.buttonText}>=</Text>

          </TouchableHighlight>

        </View>

    </View>

    </View>

  </View>

);

};


 

export default CalculatorApp;

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值