Android Studio创建简易计算器

目的:
开发一个简单的计算器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&
  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了在Android Studio设计一个简易计算器,您需要遵循以下步骤: 1.创建一个新的Android Studio项目,命名为clc666b。 2.在activity_main.xml文件中,使用网格布局管理器来设计一个居中、满屏计算器。您可以使用按钮、文本框和其他UI元素来实现这个界面。 3.在MainActivity.java文件中,编写代码来实现计算器的逻辑。您需要使用单选按钮来选择加法或乘法,然后根据用户的选择来执行相应的计算。 4.测试您的应用程序,确保它能够正确地执行加法和乘法计算。 下面是一个简单的范例代码,可以帮助您开始编写您的应用程序: ```java public class MainActivity extends AppCompatActivity { private EditText num1EditText; private EditText num2EditText; private TextView resultTextView; private RadioButton addRadioButton; private RadioButton multiplyRadioButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); num1EditText = findViewById(R.id.num1EditText); num2EditText = findViewById(R.id.num2EditText); resultTextView = findViewById(R.id.resultTextView); addRadioButton = findViewById(R.id.addRadioButton); multiplyRadioButton = findViewById(R.id.multiplyRadioButton); Button calculateButton = findViewById(R.id.calculateButton); calculateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double num1 = Double.parseDouble(num1EditText.getText().toString()); double num2 = Double.parseDouble(num2EditText.getText().toString()); double result = 0; if (addRadioButton.isChecked()) { result = num1 + num2; } else if (multiplyRadioButton.isChecked()) { result = num1 * num2; } resultTextView.setText(String.valueOf(result)); } }); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值