自己做一个计算器app

现代人离不开计算器,在手机App时代中,每个人都可以自己定制一个属于自己的计算器App。如果您也想自己动手制作一个计算器App,可以参考以下的内容。

首先,我们需要考虑我们的计算器要实现什么样的计算功能。例如,加、减、乘、除等基本运算是必须的,还要考虑到开方、平方、百分数以及大括号等高级运算。

接下来,我们需要确定使用的开发工具。目前最流行的安卓App开发工具是Android Studio,该工具是由Google开发的免费跨平台开发环境,提供丰富的开发库和工具。

接下来,我们可以开始创建一个新的Android Studio项目,然后选择空白Activity模板,我们可以命名为“calculator”。

然后在我们的activity_calculator.xml文件中,我们需要编写UI元素,并设置相应的属性。如下所示:

```

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/input_text"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:inputType="number"

android:gravity="right"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal">

android:id="@+id/button_1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="1"/>

android:id="@+id/button_2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="2"/>

android:id="@+id/button_3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="3"/>

android:id="@+id/button_add"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="+"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal">

android:id="@+id/button_4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="4"/>

android:id="@+id/button_5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="5"/>

android:id="@+id/button_6"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="6"/>

android:id="@+id/button_subtract"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="-"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal">

android:id="@+id/button_7"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="7"/>

android:id="@+id/button_8"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="8"/>

android:id="@+id/button_9"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="9"/>

android:id="@+id/button_multiply"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="*"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal">

android:id="@+id/button_clear"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="C"/>

android:id="@+id/button_0"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="0"/>

android:id="@+id/button_equals"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="="/>

android:id="@+id/button_divide"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="/"/>

```

接下来,我们需要在MainActivity.java类中编写代码,此处采用了Java语言,代码如下所示:

```

public class MainActivity extends AppCompatActivity {

TextView inputText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_calculator);

inputText = findViewById(R.id.input_text);

}

void onNumberClick(View view) {

Button button = (Button) view;

String currentText = inputText.getText().toString();

String buttonText = button.getText().toString();

inputText.setText(currentText + buttonText);

}

void onOperatorClick(View view) {

Button button = (Button) view;

String currentText = inputText.getText().toString();

String buttonText = button.getText().toString();

if (currentText.endsWith("+") || currentText.endsWith("-") || currentText.endsWith("*") || currentText.endsWith("/")) {

inputText.setText(currentText.substring(0, currentText.length() - 1) + buttonText);

} else {

inputText.setText(currentText + buttonText);

}

}

void onClearClick(View view) {

inputText.setText("");

}

void onEqualsClick(View view) {

String currentText = inputText.getText().toString();

ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");

try {

Double result = (Double) engine.eval(currentText);

inputText.setText(result.toString());

} catch (ScriptException e) {

Toast.makeText(this, "Invalid expression", Toast.LENGTH_SHORT).show();

}

}

}

```

在代码中,我们使用了Java语言的ScriptEngine来计算输入表达式的结果,并将结果显示在TextView中。同时,我们还定义了用户在UI中的交互方法,以实现用户的操作。

最后,我们可以进行测试运行。将代码编译、构建、运行在安卓设备上,就可以自己动手制作一个简单的计算器App。如果想要更加完善和优化该App,需要深入学习安卓开发知识和相关技术。


相关知识:
自学会计做账app
随着数字化的不断深入,许多传统的行业也开始向数字化、智能化转型,会计行业也不例外。现在,已经出现了许多自学会计做账App供大众使用,通过这些App,用户可以轻松地完成会计的基础操作,无需学习繁琐的会计原理,让想要自学会计的人更容易上手。自学会计做账App的
2023-06-05
自制式app制作平台
自制式app制作平台是一种能够帮助用户无需编写代码,通过可视化界面制作自己的手机应用程序的在线平台。它的原理是:通过提供图形化编辑界面和基于组件库的程序编辑和配置工具,让用户通过拖拽和配置的方式,快速制作APP应用程序,无需编码,快速上线。自制式app制作
2023-06-05
自己设计服装的app开发
自己设计服装的App是一种非常有趣和创造性的平台。特别是在今天,人们对自己的外表和穿着越来越关注,开发这样一个应用程序是非常有必要的。在这个应用程序中,用户可以使用多种工具和材料来创建自己的服装设计。这篇文章将详细介绍如何开发这样一个应用程序。1. 首先,
2023-06-05
自己想制作一个app
制作一个APP可以说是一个非常有趣和具有成就感的过程。但是,如果你不知道从哪里开始,制作一个APP可能会非常困难。在这篇文章中,我将详细介绍制作一个APP的基本原理并为你提供一些有用的建议。1.确定你的app需要解决哪些问题首先,你需要确定你的APP需要解
2023-06-05
自己怎样做自己的app
做自己的app是一个非常有趣的事情,也是一个技术含量比较高的挑战。实现的方法有很多种,但大致可以分为两种,一种是利用各种app开发平台,比如说微信小程序、阿里云移动开发等;另一种是利用专业的app开发工具,比如说Adobe AIR、PhoneGap、Xco
2023-06-05
自己开发的app有专利吗
在开发自己的app中,如何保护自己的知识产权是一个非常重要的问题。专利是一种常见的知识产权形式,它的作用是保护发明者的独特创新,防止其他人未经许可使用他们的发明。那么,开发自己的app有没有专利呢?答案是有可能的。首先,需要明确的是,专利的保护对象是发明,
2023-06-05
自己套壳开发app
自己套壳开发 APP 是一种比较常见的开发方式,它适用于那些想要快速开发 APP 的开发者。所谓套壳开发,指的是在现有的 APP 基础上进行修改,改变其中的 Logo、名称、启动页等主题元素,使得该 APP 变成另一个全新的 APP。在这篇文章里,我们将详
2023-06-05
自己可以做app商城吗
可以自己搭建一个APP商城,其原理主要就是构建一个包含产品、用户、订单和交易等核心要素的电商平台。一、技术工具APP商城的开发需要掌握前端和后端的技术。前端开发者需要掌握 HTML、CSS、JavaScript等基础技能,后端开发者需要掌握Java、PHP
2023-06-05
自贡app定制开发维护
自贡app定制开发维护是指根据客户需求,对自贡地区进行信息整合、应用开发、数据源整合等方面的定制开发服务,同时对已经开发出来的软件产品进行长期的维护服务,确保软件产品的稳定性和功能的完善性。自贡作为四川省的一个重要城市,其信息化建设已经形成了一定的规模和基
2023-06-05
使用python做appium自动化
Appium是一款用于自动化移动应用程序测试的跨平台开源框架。Appium的原理是基于WebDriver协议,使用客户端/服务器模型,将测试指令从客户端发送到服务器端,并在移动设备上执行。Appium支持iOS,Android以及Windows平台,提供丰
2023-05-31
苹果手机怎么自己开发app
苹果手机上的iOS系统真正实现了对开发者开放的调用API,它提供了强大的开发工具,是一个很好的平台,供iOS开发者使用并开发各种App。下面介绍苹果手机自己开发App的原理和步骤。1. 前置技能准备在开始iOS开发之前,你需要有基本的编程背景,熟悉至少一种
2023-05-30
捷径做app签到自动
为了方便用户使用各类app,许多app都会设计签到系统来吸引用户继续使用。而对于一些长期使用的用户,每天手动签到也会变得有些繁琐和不方便。因此,有的用户会寻找一些自动签到的方法,在不违反app使用协议的前提下,实现自动签到,以节约时间。本文将介绍一种较为简
2023-05-30
©2015-2021 自建app开发平台 www.appbyme.cn 蜀ICP备17005078号-1