自己做一个计算器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是一个帮助用户培养自律意识和习惯的手机应用程序。它提供一系列
2023-06-05
自己然后制作app
自己制作一款App是现代科技时代所具备的技能之一,也是一个复杂而考验技术深度的过程。在进行自己的App制作时,需要掌握一定的软件编程知识、设计技能和市场需求等多方面的知识。本文将从原理以及详细介绍两个方面阐述自己制作App的相关内容。一、原理1、选择软件开
2023-06-05
自己开发一个app运营起来的费用怎么算
开发一个APP需要投入一定的时间和金钱,不同的APP类型和功能对应着不同的开发成本,如果想要对APP运营的费用进行精确计算,需要从以下几个方面进行考虑:1. APP设计和开发费用APP的设计和开发费用是最基本的成本,在这部分成本中包括了项目开发、系统设计、
2023-06-05
自己制作结婚请柬的app
自己制作结婚请柬的app是一种非常实用的应用程序,可以让用户轻松快速地通过手机制作一封精美、独特的结婚请柬。具体的实现方式有多种,其中一种较为典型的实现方式是:通过应用程序内置的模板,用户只需要选择自己满意的样式,添加一些个性化的文字和图片即可生成自己的结
2023-06-05
自己做1个app
做一个app可能对初学者来说是一项艰巨的任务,但是一旦你掌握了一定的编程技能,就会发现其实并没有那么难。在这篇文章中,我想向你介绍一些开发一个app的原理和步骤。当然,这不是一种非常详细的教程,但它能够提供一个起点,让你开始理解如何创建一个基础app。第一
2023-06-05
自己做app还需要交税吗知乎
在中国,如果你作为个人或者企业自己开发或制作APP,并在APP Store或各大应用市场上线并获取了收入或盈利,那么你是需要缴纳税款的。首先,App的收入主要来自于两种途径:广告费和应用内购买。对于广告费收入,如果你的应用在国内市场运营并且广告主为国内的企
2023-06-05
自动精灵app脚本制作
自动精灵app是一款非常实用的自动化工具,能够通过简单地对手机进行设置,实现自动化执行任务的功能。其实现原理主要是通过脚本的方式来实现,因此本文将对自动精灵app脚本制作进行详细介绍。一、自动精灵app概述自动精灵app是一款Android平台上的自动化软
2023-06-05
自动制作动画的app
近年来,随着科技的不断发展,越来越多的自动化工具被应用到各个领域中,包括动画制作领域。自动制作动画的应用程序也随之问世,它们可以使动画制作变得更加简单和高效。本文将介绍一些自动制作动画的应用程序以及它们的原理。一、自动制作动画的应用程序1. Plotago
2023-06-05
自动化设备app开发
随着信息技术和通信技术的不断发展,自动化设备的智能化程度越来越高,自动化设备app的开发也变得越来越重要。自动化设备app是指运行于智能手机或平板电脑等移动设备上,与自动化设备相连接,提供实时监控、操作控制、状态异常预警等服务的应用程序软件。自动化设备ap
2023-06-05
企业开发自己的app软件
随着智能手机和移动互联网的普及,企业开发自己的app软件已成为越来越多企业的选择。企业开发自己的app软件可以让企业更好地与客户进行沟通和互动,提高品牌知名度和客户黏性,并且还可以有效提高企业内部的工作效率和协同效率。下面将详细介绍企业开发自己的app软件
2023-05-30
媒体自建app
媒体自建APP是指媒体机构或个人通过开发自己的APP,实现自身品牌推广、流量变现、用户服务等一系列目标。随着移动互联网的普及,越来越多的媒体开始考虑建设自己的APP,以满足用户多元化的需求,并在市场竞争中保持竞争力。本文将从原理及详细介绍两个方面来进行阐述
2023-05-30
程序员开发app财务自由吗
程序员开发app是一种非常热门的职业,在这个行业里,app的开发和销售是一项高薪工作。然而这个行业却不一定能够给程序员带来财务自由。下面我们来详细介绍一下这个问题。首先,程序员开发app需要有一定的技术水平,具备相关知识和经验,这需要大量的时间、精力和金钱
2023-05-30
©2015-2021 自建app开发平台 www.appbyme.cn 蜀ICP备17005078号-1