当前位置:   article > 正文

Android Studio创建简易计算器_利用android stdio设计一个计算器实现乘方、开方

利用android stdio设计一个计算器实现乘方、开方

目的:
开发一个简单的计算器App,使之能够完成加减乘除混合运算工具及环境使用java语言,在Android studio平台上进行开发

功能设计:
“+”:实现两数相加
“-”:实现两数相减

“*”:实现两数相乘

“/”:实现两数相除

“=”:计算并得出正确结果

“AC”:清屏

“Del”:清除

 

设计思路:
首先设计一个可视化的界面,供用户输入数据并查看结果。用户可通过点击相应按钮输入正确的表达式,最后按"="得出正确结果。在计算过程中可以通过点击“Del”键修改输入内容,在进行下一次的运算之前必须先进行清零操作。计算器可以实现基础的整数和小数点的加减乘除运算,界面设计参考普通计算器,用LinearLayout布局实现界面排版。

 

代码:

Activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <EditText
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="40sp"
            android:enabled="false"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/cls"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="AC"
                android:background="#3f3c3c"
                android:textColor="#ffffff"/>
            <Button
                android:id="@+id/Backspace"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="Del"
                android:textAllCaps="false"
                android:background="#3f3c3c"
                android:textColor="#ffffff"/>
    
            <Button
                android:id="@+id/mul"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="*"
                android:background="#666666"
                android:textColor="#ffffff"/>
            <Button
                android:id="@+id/div"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="/"
                android:background="#666666"
                android:textColor="#ffffff"/>
    
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/seven"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="7"
                android:background="#666666"
                android:textColor="#ffffff"/>
            <Button
                android:id="@+id/eight"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="8"
                android:background="#666666"
                android:textColor="#ffffff"/>
            <Button
                android:id="@+id/nine"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="9"
                android:background="#666666"
                android:textColor="#ffffff"/>
            <Button
                android:id="@+id/sub"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="-"
                android:background="#666666"
                android:textColor="#ffffff"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/four"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="4"
                android:background="#666666"
                android:textColor="#ffffff"/>
            <Button
                android:id="@+id/five"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="5"
                android:background="#666666"
                android:textColor="#ffffff"/>
            <Button
                android:id="@+id/six"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="6"
                android:background="#666666"
                android:textColor="#ffffff"/>
            <Button
                android:id="@+id/add"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="20sp"
                android:text="+"
                android:background="#666666"
                android:textColor="#ffffff"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height&
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/223268
推荐阅读
相关标签
  

闽ICP备14008679号