江鸟中原145——计算器实现 原创

wx656dc05273a9a
发布于 2023-12-10 16:33
浏览
0收藏

大家好,我是江鸟中原鸿蒙展翅,今天给大家展示,计算器是电脑和手机都必不可少的一款软件,今天分享在鸿蒙系统上开发计算器软件。![20210702162006763.png]

先看功能效果:
江鸟中原145——计算器实现-鸿蒙开发者社区江鸟中原145——计算器实现-鸿蒙开发者社区江鸟中原145——计算器实现-鸿蒙开发者社区

这里演示了一下加法,该计算器中实现了加减乘除求余,开根号,倒数等所有功能。其他功能的演示这里就不浪费篇幅了。

接下来看界面的设计,这里设计了两种边框主题,一个是显示计算过程和结果的两个Text组件的边框,在graphic文件夹下新建xml文件:

<?xml version=“1.0” encoding=“utf-8”?>
<shape
xmlns:ohos=“http://schemas.huawei.com/res/ohos
ohos:shape=“rectangle”>
<corners
ohos:radius=“20”
/>
<solid
ohos:color=“#FFffff”/>
<stroke
ohos:color=“#0066FF”
ohos:width=“10”/>
</shape>
再新建一个xml文件,设计按钮的背景样式。

<?xml version=“1.0” encoding=“utf-8”?>
<shape
xmlns:ohos=“http://schemas.huawei.com/res/ohos
ohos:shape=“rectangle”>
<corners
ohos:radius=“20”
/>
<solid
ohos:color=“#FF5151EC”/>
</shape>
接下来就是计算器的界面搭建的xml代码:

<?xml version=“1.0” encoding=“utf-8”?>
<DirectionalLayout
xmlns:ohos=“http://schemas.huawei.com/res/ohos
ohos:height=“match_parent”
ohos:width=“match_parent”
ohos:orientation=“vertical”>

<Text
    ohos:height="60fp"
    ohos:width="match_parent"
    ohos:background_element="#00f"
    ohos:padding="10fp"
    ohos:text="计算器"
    ohos:text_alignment="center"
    ohos:text_color="#fff"
    ohos:text_size="30fp"/>

<TableLayout
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:column_count="1"
    ohos:orientation="vertical"
    ohos:padding="10fp"
    ohos:row_count="8">

    <DirectionalLayout
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:background_element="$graphic:background_textfield"
        ohos:orientation="vertical">
        <Text
            ohos:id="$+id:tv_jsq1"
            ohos:height="40fp"
            ohos:width="match_parent"
            ohos:text_color="#000"
            ohos:hint="请在按键上输入计算的数字"
            ohos:hint_color="#aaa"
            ohos:padding="5fp"
            ohos:text_alignment="vertical_center|right"
            ohos:text_size="20fp"/>

        <Text
            ohos:id="$+id:tv_jsq2"
            ohos:height="40fp"
            ohos:width="match_parent"
            ohos:text_color="#000"
            ohos:hint="请在按键上输入计算的数字"
            ohos:hint_color="#aaa"
            ohos:padding="5fp"
            ohos:text_alignment="vertical_center|right"
            ohos:text_size="25fp"/>
    </DirectionalLayout>

    <DirectionalLayout
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:alignment="horizontal_center"
        ohos:orientation="horizontal"
        ohos:top_margin="10fp">

        <Button
            ohos:id="$+id:btn_jsq_1"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="MC"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_2"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="MR"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_3"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="MS"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_4"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="M+"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_5"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="-"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>
    </DirectionalLayout>

    <DirectionalLayout
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:alignment="horizontal_center"
        ohos:orientation="horizontal">

        <Button
            ohos:id="$+id:btn_jsq_6"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="←"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_7"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="CE"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_8"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="C"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_9"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="±"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_10"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="√"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>
    </DirectionalLayout>

    <DirectionalLayout
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:alignment="horizontal_center"
        ohos:orientation="horizontal">

        <Button
            ohos:id="$+id:btn_jsq_11"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="7"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_12"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="8"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_13"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="9"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_14"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="/"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_15"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="%"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>
    </DirectionalLayout>

    <DirectionalLayout
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:alignment="horizontal_center"
        ohos:orientation="horizontal">

        <Button
            ohos:id="$+id:btn_jsq_16"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="4"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_17"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="5"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_18"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="6"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_19"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="*"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>

        <Button
            ohos:id="$+id:btn_jsq_20"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="1/X"
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>
    </DirectionalLayout>

    <DirectionalLayout
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:alignment="center"
        ohos:orientation="horizontal">

        <DirectionalLayout
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:orientation="vertical"
            ohos:weight="5">

            <DirectionalLayout
                ohos:height="match_content"
                ohos:width="match_parent"
                ohos:alignment="center"
                ohos:orientation="horizontal">

                <Button
                    ohos:id="$+id:btn_jsq_21"
                    ohos:height="match_content"
                    ohos:width="match_content"
                    ohos:background_element="$graphic:background_button_jsq"
                    ohos:margin="5fp"
                    ohos:padding="10fp"
                    ohos:text="1"
                    ohos:text_color="#fff"
                    ohos:text_size="22fp"
                    ohos:weight="1"/>

                <Button
                    ohos:id="$+id:btn_jsq_22"
                    ohos:height="match_content"
                    ohos:width="match_content"
                    ohos:background_element="$graphic:background_button_jsq"
                    ohos:margin="5fp"
                    ohos:padding="10fp"
                    ohos:text="2"
                    ohos:text_color="#fff"
                    ohos:text_size="22fp"
                    ohos:weight="1"/>

                <Button
                    ohos:id="$+id:btn_jsq_23"
                    ohos:height="match_content"
                    ohos:width="match_content"
                    ohos:background_element="$graphic:background_button_jsq"
                    ohos:margin="5fp"
                    ohos:padding="10fp"
                    ohos:text="3"
                    ohos:text_color="#fff"
                    ohos:text_size="22fp"
                    ohos:weight="1"/>

                <Button
                    ohos:id="$+id:btn_jsq_24"
                    ohos:height="match_content"
                    ohos:width="match_content"
                    ohos:background_element="$graphic:background_button_jsq"
                    ohos:margin="5fp"
                    ohos:padding="10fp"
                    ohos:text="-"
                    ohos:text_color="#fff"
                    ohos:text_size="22fp"
                    ohos:weight="1"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:height="match_content"
                ohos:width="match_parent"
                ohos:alignment="center"
                ohos:orientation="horizontal">

                <Button
                    ohos:id="$+id:btn_jsq_25"
                    ohos:height="match_content"
                    ohos:width="match_content"
                    ohos:background_element="$graphic:background_button_jsq"
                    ohos:margin="5fp"
                    ohos:padding="10fp"
                    ohos:text="0"
                    ohos:text_color="#fff"
                    ohos:text_size="22fp"
                    ohos:weight="2"/>

                <Button
                    ohos:id="$+id:btn_jsq_26"
                    ohos:height="match_content"
                    ohos:width="match_content"
                    ohos:background_element="$graphic:background_button_jsq"
                    ohos:margin="5fp"
                    ohos:padding="10fp"
                    ohos:text="."
                    ohos:text_color="#fff"
                    ohos:text_size="22fp"
                    ohos:weight="1"/>

                <Button
                    ohos:id="$+id:btn_jsq_27"
                    ohos:height="match_content"
                    ohos:width="match_content"
                    ohos:background_element="$graphic:background_button_jsq"
                    ohos:margin="5fp"
                    ohos:padding="10fp"
                    ohos:text="+"
                    ohos:text_color="#fff"
                    ohos:text_size="22fp"
                    ohos:weight="1"/>
            </DirectionalLayout>
        </DirectionalLayout>

        <Button
            ohos:id="$+id:btn_jsq_28"
            ohos:height="match_parent"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button_jsq"
            ohos:margin="5fp"
            ohos:padding="10fp"
            ohos:text="="
            ohos:text_color="#fff"
            ohos:text_size="22fp"
            ohos:weight="1"/>
    </DirectionalLayout>
</TableLayout>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
  • 298.
  • 299.
  • 300.
  • 301.
  • 302.
  • 303.
  • 304.
  • 305.
  • 306.
  • 307.
  • 308.
  • 309.
  • 310.
  • 311.
  • 312.
  • 313.
  • 314.
  • 315.
  • 316.
  • 317.
  • 318.
  • 319.
  • 320.
  • 321.
  • 322.
  • 323.
  • 324.
  • 325.
  • 326.
  • 327.
  • 328.
  • 329.
  • 330.
  • 331.
  • 332.
  • 333.
  • 334.
  • 335.
  • 336.
  • 337.
  • 338.
  • 339.
  • 340.
  • 341.
  • 342.
  • 343.
  • 344.
  • 345.
  • 346.
  • 347.
  • 348.
  • 349.
  • 350.
  • 351.
  • 352.
  • 353.
  • 354.
  • 355.
  • 356.
  • 357.
  • 358.
  • 359.
  • 360.
  • 361.
  • 362.
  • 363.
  • 364.
  • 365.
  • 366.
  • 367.
  • 368.
  • 369.
  • 370.
  • 371.
  • 372.
  • 373.
  • 374.
  • 375.
  • 376.
  • 377.
  • 378.
  • 379.
  • 380.
  • 381.
  • 382.
  • 383.
  • 384.
  • 385.
  • 386.
  • 387.
  • 388.
  • 389.
  • 390.
  • 391.
  • 392.
  • 393.
  • 394.
  • 395.
  • 396.
  • 397.
  • 398.
  • 399.
  • 400.
  • 401.
  • 402.
  • 403.
  • 404.
  • 405.
  • 406.
  • 407.
  • 408.
  • 409.
  • 410.
  • 411.
  • 412.
  • 413.
  • 414.
  • 415.
  • 416.
  • 417.
  • 418.
  • 419.
  • 420.
  • 421.
  • 422.
  • 423.
  • 424.
  • 425.
  • 426.
  • 427.
  • 428.
  • 429.
  • 430.
  • 431.
  • 432.
  • 433.
  • 434.
  • 435.
  • 436.
  • 437.
  • 438.
  • 439.

</DirectionalLayout>

整个布局页面中使用了DirectionalLayout(线性布局),TableLayout(表格布局)的综合使用,里面并嵌套使用了Text和Button组件。

接下来就是实现计算功能的java代码了,

package com.example.hm_phone_java.slice;

import com.example.hm_phone_java.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class JSQAbilitySlice extends AbilitySlice {
private int[] ids={
ResourceTable.Id_btn_jsq_1,ResourceTable.Id_btn_jsq_2,
ResourceTable.Id_btn_jsq_3,ResourceTable.Id_btn_jsq_4,
ResourceTable.Id_btn_jsq_5,ResourceTable.Id_btn_jsq_6,
ResourceTable.Id_btn_jsq_7,ResourceTable.Id_btn_jsq_8,
ResourceTable.Id_btn_jsq_9,ResourceTable.Id_btn_jsq_10,
ResourceTable.Id_btn_jsq_11,ResourceTable.Id_btn_jsq_12,
ResourceTable.Id_btn_jsq_13,ResourceTable.Id_btn_jsq_14,
ResourceTable.Id_btn_jsq_15,ResourceTable.Id_btn_jsq_16,
ResourceTable.Id_btn_jsq_17,ResourceTable.Id_btn_jsq_18,
ResourceTable.Id_btn_jsq_19,ResourceTable.Id_btn_jsq_20,
ResourceTable.Id_btn_jsq_21,ResourceTable.Id_btn_jsq_22,
ResourceTable.Id_btn_jsq_23,ResourceTable.Id_btn_jsq_24,
ResourceTable.Id_btn_jsq_25,ResourceTable.Id_btn_jsq_26,
ResourceTable.Id_btn_jsq_27,ResourceTable.Id_btn_jsq_28
};
private Button[] btns=new Button[ids.length];
private boolean firstKey=false;
private Text label,label2;
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
this.setUIContent(ResourceTable.Layout_ability_jsq);
label= (Text) this.findComponentById(ResourceTable.Id_tv_jsq1);
label2= (Text) this.findComponentById(ResourceTable.Id_tv_jsq2);
for (int i = 0; i < ids.length; i++) {
btns[i]= (Button) this.findComponentById(ids[i]);
btns[i].setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Button button= (Button) component;
String text=button.getText();
if (text.equals(“CE”)) {
// 用户按了"CE"键
label2.setText(“0”);
label.setText(“0”);
firstKey = true;
operator = “=”;
issqrt = true;
} else if (“0123456789.”.indexOf(text) >= 0) {
// 用户按了数字键或者小数点键
handleNumber(text);
// handlezero(zero);
} else {
// 用户按了运算符键
handleOperator(text);
}
}
});
}
}
/**
* 处理数字键被按下的事件
*
* @param key
*/
private void handleNumber(String key) {
if (firstKey) {
// 输入的第一个数字
if ((key.equals(“.”)) && (label2.getText().indexOf(“.”) < 0)) {
label2.setText(label2.getText() + “.”);
} else {
if (key.equals(“.”)) {
label2.setText(“0” + key);
} else {
label2.setText(key);
}
}
} else if ((key.equals(“.”)) && (label2.getText().indexOf(“.”) < 0)) {
// 输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
label2.setText(label2.getText() + “.”);
} else if (!key.equals(“.”)) {
// 如果输入的不是小数点,则将数字附在结果文本框的后面
label2.setText(label2.getText() + key);
}
// 以后输入的肯定不是第一个数字了
firstKey = false;
}

String operator = "=";
// 是否合法
boolean operateValidFlag = true;
// 计算中间 1+3-2
private double resultNum = 0.0;
private boolean issqrt = true;

/**
 * 处理运算符键被按下的事件
 *
 * @param key
 */
private void handleOperator(String key) {
    if (operator.equals("/")) {
        // 除法运算
        // 如果当前结果文本框中的值等于0
        if (getNumberFromText() == 0.0) {
            // 操作不合法
            operateValidFlag = false;
            label2.setText("除数不能为零");
        } else {
            resultNum /= getNumberFromText();
        }
    } else if (operator.equals("1/X")) {
        // 倒数运算
        if (resultNum == 0.0) {
            // 操作不合法
            operateValidFlag = false;
            label2.setText("零没有倒数");
        } else {
            resultNum = 1 / resultNum;
        }
    } else if (operator.equals("+")) {
        // 加法运算
        resultNum += getNumberFromText();
    } else if (operator.equals("-")) {
        // 减法运算
        resultNum -= getNumberFromText();
    } else if (operator.equals("*")) {
        // 乘法运算
        resultNum *= getNumberFromText();
    } else if (operator.equals("√")) {
        // 平方根运算
        if (resultNum < 0) {
            issqrt = false;
            label2.setText("无效输入");
            resultNum = 0;
        } else {
            resultNum = Math.sqrt(resultNum);
        }

    } else if (operator.equals("%")) {
        // 百分号运算,除以100
        resultNum = resultNum / 100;

    } else if (operator.equals("±")) {
        // 正数负数运算
        resultNum = resultNum * (-1);
    } else if (operator.equals("=")) {
        // 赋值运算
        resultNum = getNumberFromText();

    }
    if (operateValidFlag) {
        // 双精度浮点数的运算
        long t1;
        double t2;
        t1 = (long) resultNum;
        t2 = resultNum - t1;
        if (t2 == 0) {
            if (issqrt) {
                label2.setText(String.valueOf(t1));
            }
            if (key.equals("=")) {
                label.setText("0");
            } else {
                label.setText(label2.getText() + key);
            }
        } else {
            label2.setText(String.valueOf(resultNum));
            if (key.equals("=")) {
                label.setText("0");
            } else {
                label.setText(label2.getText() + key);
            }
        }
    }
    // 运算符等于用户按的按钮
    operator = key;
    firstKey = true;
    operateValidFlag = true;
}

/**
 * 从结果文本框中获取数字
 *
 * @return
 */
private double getNumberFromText() {
    double result = 0;
    try {
        result = Double.valueOf(label2.getText()).doubleValue();
    } catch (NumberFormatException e) {
    }
    return result;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.

}

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
收藏
回复
举报


回复
    相关推荐