Andorid实现简单计算器功能

我使用的是线性布局,也可以使用表格布局,但是暂时还没有学到

首先来看一下布局格式代码,直接将代码写在activity_main中:

<?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">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">

        <EditText
            android:id="@+id/re"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@null" />
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">


        <Button
            android:id="@+id/b1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/b2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/b3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="3"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/b4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="/"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <Button
            android:id="@+id/b5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="4"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/b6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="5"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/b7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            androi
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是简易计算器实现步骤: 1. 创建一个新的Android Studio项目,并在布局文件中添加一个TextView和多个Button。 2. 在MainActivity.java文件中,定义一个字符串变量来存储用户输入的数字和操作符,并在每个按钮的点击事件中更新该变量。 3. 创建一个名为calculate()的方法,该方法将用户输入的字符串解析为数字和操作符,并执行相应的计算。最后,将结果显示在TextView中。 4. 在每个按钮的点击事件中调用calculate()方法,以便在用户按下“=”按钮时执行计算。 5. 运行应用程序并测试它是否按预期工作。 希望这能帮助到你! ### 回答2: Android Studio 是常用的Android应用程序开发环境,利用该工具可以开发完整的Android应用程序,其中也包括实现简易计算器。下面我们就来介绍如何通过 Android Studio 实现简易计算器。 1.创建新的Android项目:我们打开Android Studio,选择“Start a new Android Studio project”,该选项将指导我们创建一个新的Android项目。 2.设计用户界面:我们需要使用XML文件描述计算器的用户界面,主要包含数字、运算符等键,在这里,我们需要利用Android提供的界面设计工具 -- Android Studio Design View 。 3.编写Java代码:在Android Studio中,Java代码对应着Java类文件,因此我们需要新建一个类文件,将所有的计算逻辑代码编写在其中。 4.添加计算器逻辑:我们需要编写代码,实现基本运算:加、减、乘、除、取余等。 5.添加响应事件:我们需要为计算器中的数字、运算符等键添加响应事件,实现点击后数字和运算符的显示。 6.测试程序:我们需要在模拟器或者真机上测试我们开发计算器程序,保证其在使用时可以正常运行。 综上所述,通过以上步骤,我们可以利用Android Studio实现一个简单的计算器。编写一个计算器的过程中,需要了解常见的布局,如LinearLayout、RelativeLayout等,并可以利用著名的MVP模式,将View与Presenter分离,提高代码的可读性和可维护性。此外,还需要掌握如何实现适应不同分辨率的布局,保证在各种机型上都能够良好地运行。 ### 回答3: Android Studio 是一款强大的 Android 应用开发软件,可以帮助我们创建各种 Android 应用,包括计算器应用。下面将详细介绍如何利用 Android Studio 实现简易计算器。 第一步:创建项目 在 Android Studio 中点击“Start a new Android Studio project”,选择一个名字和保存路径,接着选择 Empty Activity,点击“Finish”创建项目。 第二步:设计计算器界面 在 activity_main.xml 文件中,我们可以选择布局元素创建界面。在这里,我们使用LinearLayout作为容器,从而创建我们的界面元素。具体的代码如下: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:background="@color/cardview_dark_background" android:gravity="right" android:textAppearance="?android:textAppearanceLarge" android:textColor="@color/white" android:textSize="50sp" android:textStyle="bold" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="4" > <Button android:id="@+id/btn_clear" android:layout_width="0dp" android:layout_height="80dp" android:layout_weight="1" android:background="@color/cardview_dark_background" android:text="C" android:textAllCaps="false" android:textAppearance="?android:textAppearanceLarge" android:textColor="@color/white" android:textSize="20sp" /> ...... </LinearLayout> </LinearLayout> ``` 这里我们用 LinearLayout 创建一个垂直方向的布局,然后再在垂直布局中用 LinearLayout 创建一个水平方向的布局。在这个水平方向的布局添加数值、运算符和清除按钮,具体代码如下: ``` <Button android:id="@+id/btn_clear" android:layout_width="0dp" android:layout_height="80dp" android:layout_weight="1" android:background="@color/cardview_dark_background" android:text="C" android:textAllCaps="false" android:textAppearance="?android:textAppearanceLarge" android:textColor="@color/white" android:textSize="20sp" /> <TextView android:id="@+id/btn_divide" android:layout_width="0dp" android:layout_height="80dp" android:layout_weight="1" android:background="@color/cardview_dark_background" android:gravity="center" android:text="/" android:textAppearance="?android:textAppearanceLarge" android:textColor="@color/white" android:textSize="20sp" /> <Button android:id="@+id/btn_multiply" android:layout_width="0dp" android:layout_height="80dp" android:layout_weight="1" android:background="@color/cardview_dark_background" android:text="*" android:textAllCaps="false" android:textAppearance="?android:textAppearanceLarge" android:textColor="@color/white" android:textSize="20sp" /> ...... ``` 第三步:实现计算器逻辑 在 MainActivity 类中实现计算器的逻辑。首先,我们需要定义一个数字输入框(TextView)用来显示操作数,同时在代码中对输入数字和操作符进行处理,并显示结果。 具体代码如下: ``` public class MainActivity extends AppCompatActivity { private TextView tvInput; private String lastOperator = ""; private double operand1 = Double.NaN; private double operand2; private final char ADDITION = '+'; private final char SUBTRACTION = '-'; private final char MULTIPLICATION = '*'; private final char DIVISION = '/'; private final char EQUALS = '='; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvInput = findViewById(R.id.tv_input); tvInput.setText(""); } public void onButtonClick(View view){ Button btn = (Button)view; String str = btn.getText().toString(); if(str.equals("C")){ tvInput.setText(""); lastOperator=""; operand1=Double.NaN; }else if(str.equals("+/-")){ double value = Double.parseDouble(tvInput.getText().toString()); value = -value; tvInput.setText(String.valueOf(value)); }else if(str.equals("%")){ if(lastOperator.equals("") && !tvInput.getText().toString().equals("")){ double value = Double.parseDouble(tvInput.getText().toString()); value = value/100; tvInput.setText(String.valueOf(value)); } }else if(str.equals(".")){ if(lastOperator.equals("") && !tvInput.getText().toString().contains(".")){ tvInput.append("."); } }else if(isOperator(str.charAt(0))){ if(!lastOperator.equals("") && tvInput.getText().toString().equals("")){ return; } if(Double.isNaN(operand1)){ operand1 = Double.parseDouble(tvInput.getText().toString()); }else{ operand2 = Double.parseDouble(tvInput.getText().toString()); switch(lastOperator){ case "+": operand1 = operand1 + operand2; break; case "-": operand1 = operand1 - operand2; break; case "*": operand1 = operand1 * operand2; break; case "/": operand1 = operand1 / operand2; break; case "=": break; } } lastOperator = String.valueOf(str.charAt(0)); tvInput.setText(""); }else if(str.equals("=")){ if(lastOperator.equals("")) return; if(Double.isNaN(operand1)){ operand1 = Double.parseDouble(tvInput.getText().toString()); }else{ operand2 = Double.parseDouble(tvInput.getText().toString()); switch(lastOperator){ case "+": operand1 = operand1 + operand2; break; case "-": operand1 = operand1 - operand2; break; case "*": operand1 = operand1 * operand2; break; case "/": operand1 = operand1 / operand2; break; case "=": break; } } lastOperator = "="; tvInput.setText(String.valueOf(operand1)); operand1 = Double.NaN; }else{ tvInput.append(str); } } public boolean isOperator(char c){ return (c==ADDITION || c==SUBTRACTION || c== MULTIPLICATION || c == DIVISION); } } ``` 在这个代码中,“C”按钮可以清除所有输入,“+/-”按钮可以将数字转换成负数,“%”按钮可以将数字转换成百分数,“.”按钮可以添加小数点,“=”按钮可以计算输入并输出结果。 最后,在 mainifests/AndroidManifest.xml 文件中增加声明,以允许用户在应用内打开横屏模式。 ``` <activity android:name=".MainActivity" android:screenOrientation="sensorLandscape"> </activity> ``` 现在我们已经创建了一个简单的计算器应用程序。当用户在应用中输入一个算术表达式,该程序会自动计算结果并在应用窗口中显示这个结果。当然,这还只是计算器的基本功能,您还可以添加各种其他功能,如科学计算器等等。希望这篇文章能够帮助你了解如何使用 Android Studio 来实现一个简单的计算器

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值