可以自己制作的计算器app

自己制作一个计算器app,其实是一项非常有趣的事情。在这个过程中,你将学习到如何使用各种编程工具,如何使用不同的UI元素和如何将它们组合起来实现一个计算器app。 在这里,我将向你介绍如何制作一个简单的计算器app,并提供一些基础的知识和技巧。

首先,你需要一个编程环境来编写代码。这里推荐使用 Android Studio 进行开发。Android Studio 是 Google 开发的官方 Android 应用程序开发 IDE,可在 Windows、Mac OS 和 Linux 三种操作系统中运行。你可以从官方网站上下载并安装,然后根据提示进行设置和配置。

当你完成环境的配置后,就可以开始编写代码了。在 Android Studio 首次启动时,你会看到一个“Welcome to Android Studio” 窗口。点击“Start a new Android Studio project”进行创建新项目。

接着,进入“New Project”的窗口,开始为计算器app进行设置。你需要为你的项目指定一个名称,选择一个适当的应用程序 ID,选择最低兼容的 Android 版本以及选择一个空的 Activity 作为你的启动器。

等待项目创建完成后,它将自动打开一个代码窗口。在这里,你要打开 xml 文件夹,找到 activity_main.xml 文件。这是你的主屏幕布局文件。你将在此布局中创建计算器 UI 元素。

现在,你已经打开了计算器的UI布局文件,然后你需要在布局中添加 UI 元素。首先要添加的UI元素是 TextView 元素。TextView是用于显示当前计算器中的输入和输出结果的UI元素。在 activity_main.xml 布局文件中,添加以下代码:

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="30sp"

android:text="0"

android:textAlignment="textEnd"/>

其中,android:id是唯一的 ID,用于在代码中引用该元素。android:textSize属性用于设置textview的字体大小。

接下来,添加 Button 元素来实现计算器的按键。在 activity_main.xml 布局文件中,添加以下代码:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/button7"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="7"

android:textSize="30sp"/>

android:id="@+id/button8"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="8"

android:textSize="30sp"/>

android:id="@+id/button9"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="9"

android:textSize="30sp"/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/button4"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="4"

android:textSize="30sp"/>

android:id="@+id/button5"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="5"

android:textSize="30sp"/>

android:id="@+id/button6"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="6"

android:textSize="30sp"/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/button1"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="1"

android:textSize="30sp"/>

android:id="@+id/button2"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="2"

android:textSize="30sp"/>

android:id="@+id/button3"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="3"

android:textSize="30sp"/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/buttonadd"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="+"

android:textSize="30sp"/>

android:id="@+id/button0"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="0"

android:textSize="30sp"/>

android:id="@+id/buttonsub"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="-"

android:textSize="30sp"/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/buttonmul"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="x"

android:textSize="30sp"/>

android:id="@+id/buttonClear"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="C"

android:textSize="30sp"/>

android:id="@+id/buttondiv"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="100dp"

android:text="÷"

android:textSize="30sp"/>

其中,android:layout_width和android:layout_height用于设置UI元素的宽度和高度,android:layout_weight是分配剩余空间的方式,使元素根据分配的比例分配剩余空间。您还可以为每个按键分配唯一的 ID,以便在后续的代码中引用它们。

现在,你已经为计算器的 UI 布局创建了 TextView 和 Button 元素。接下来,你需要编写 Java 代码来处理 UI 元素之间的交互。

在 MainActivity.java 文件中,先定义每个 UI 元素及其 ID:

private TextView mtextView;

private Button mbutton0, mbutton1, mbutton2, mbutton3, mbutton4, mbutton5, mbutton6, mbutton7, mbutton8, mbutton9;

private Button mbuttonadd, mbuttonsub, mbuttonmul, mbuttondiv, mbuttonClear;

使用 findViewById() 方法获取每个 UI 元素的引用:

mtextView = (TextView) findViewById(R.id.textView);

mbutton0 = (Button) findViewById(R.id.button0);

mbutton1 = (Button) findViewById(R.id.button1);

mbutton2 = (Button) findViewById(R.id.button2);

mbutton3 = (Button) findViewById(R.id.button3);

mbutton4 = (Button) findViewById(R.id.button4);

mbutton5 = (Button) findViewById(R.id.button5);

mbutton6 = (Button) findViewById(R.id.button6);

mbutton7 = (Button) findViewById(R.id.button7);

mbutton8 = (Button) findViewById(R.id.button8);

mbutton9 = (Button) findViewById(R.id.button9);

mbuttonadd = (Button) findViewById(R.id.buttonadd);

mbuttonsub = (Button) findViewById(R.id.buttonsub);

mbuttonmul = (Button) findViewById(R.id.buttonmul);

mbuttondiv = (Button) findViewById(R.id.buttondiv);

mbuttonClear = (Button) findViewById(R.id.buttonClear);

接下来,使用这些 UI 元素的OnClickListener 方法来编写代码来实现计算器的按键交互操作。例如,当按下数字键时,将 digit 追加到当前值文本视图的末尾:

private void appendDigit(String digit) {

String text = mtextView.getText().toString();

if (text.equals("0")) {

text = "";

}

text += digit;

mtextView.setText(text);

}

当按下操作键时,将当前值设置为暂存值,并将操作符存储在变量中。将当前值文本视图清空,以准备下一个输入值:

private void performOperator(String operator) {

mValue = Double.parseDouble(mtextView.getText().toString());

mOperator = operator;

mtextView.setText("0");

}

最后,在 onCreate() 方法中,将 OnClickListener 分配给每个按钮:

mbutton0.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

appendDigit("0");

}

});

mbutton1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

appendDigit("1");

}

});

mbutton2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

appendDigit("2");

}

});

mbutton3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

appendDigit("3");

}

});

... // and so on

如此一来,你已经完成了自己制作的计算器app,你可以在 Android Studio 中进行运行、测试和调试。最后,你可以将应用程序打包并发布到 Google Play 商店,与世界分享你的超酷计算器会是一件非常令人兴奋的事情!


相关知识:
自主制作app
随着智能手机的普及,无论是个人用户还是企业用户都开始越来越多地使用移动应用程序(APP)。如今,可以看到的几乎所有的企业都已经推出了自己的APP,以满足客户的需要。那么,如何实现自主制作APP呢?下面,将对其进行原理和详细介绍。1.制作APP的原理制作AP
2023-06-05
自由开发者app
自由开发者App是指由个人或小组独立开发的能够满足特定功能需求的应用程序,其开发过程主要依赖于互联网上公开的各种开源技术和资源。一般来说,自由开发者App的制作团队通常由数名开发工程师组成,他们使用各种编程语言和开发工具,通过编写源代码并使用集成开发环境(
2023-06-05
自建团队开发一款app的成本
开发一款APP的成本因很多因素而异,例如想要开发哪种类型的APP,需要哪些特定功能和多少高级功能,还有是加入广告或收费等等。在此我们将介绍开发一款简单APP的成本预算,包括硬件,软件和人力成本,以及其他可能的因素。硬件成本:在开发APP时需要使用一些硬件设
2023-06-05
自己做的app需要交税吗安全吗
在介绍自己做的app是否需要交税之前,先来了解下什么是税,税是政府为了筹集社会资金而对市民征收的一种定期向政府缴存资金的制度。不同地区、国家的税收政策也存在着差异。比如在中国,不同类型的app都存在着需要交税的情况。那么自己做的app需要交税吗?根据国家税
2023-06-05
自己做一个app怎么让别人也能用呢
要让别人也能用我们做的app,需要将它发布到应用商店或是通过其他方式进行分发。下面将详细介绍一下发布到应用商店的流程以及注意事项。1. 开发者账号在发布app之前,需要注册一个开发者账号。不同的应用商店会有不同的注册要求,但大多数都需要开发者提供一些基本信
2023-06-05
自己开发一个安卓app
开发安卓应用程序需要掌握Java编程语言和Android SDK。本文将向您介绍如何开发你自己的Android App。第一步:设置开发环境在开始开发自己的Android应用程序之前,您需要选择使用哪种开发工具。Android Studio是Android
2023-06-05
自己开发一个app
App(Application)是指运行在手机、平板电脑等智能终端上的应用程序。现如今,随着智能终端的普及和人们生活方式的改变,App已成为我们日常生活的必需品。随着市场竞争的日益激烈,越来越多的人开始思考如何开发自己的App。本文就将介绍App开发的原理
2023-06-05
自己制作app的软件
制作app的软件通常被称作"应用生成器"或"应用制作者",这些软件通常采用拖放式的用户界面,并具有一定的编程能力。使用这些工具,即使您没有编程经验,也可以制作自己的应用程序。下面将为大家介绍一些应用生成器的原理和操作步骤。应用生成器通常采用所谓的“视觉化编
2023-06-05
怎样自做applepen
Apple pen(苹果笔)是一种手写笔,与iPad等设备搭配使用,可以完成手写、画图、表格填写等操作。自己动手制作一支apple pen,可以实现自己的设想,并且也比市售的价格更实惠。下面就来详细介绍一下如何自制apple pen。Apple pen的原
2023-06-05
怎么在手帐app上做自我介绍手帐
做一个自我介绍手帐可以帮助你更好地了解自己,让你有机会回顾自己的成长历程和成就,并将这些归纳总结,以便更好地规划未来。在手帐app上做自我介绍手帐非常简单,下面详细介绍一下原理和步骤。## 原理自我介绍手帐的原理是将个人的信息和成就整理归纳,以方便个人更好
2023-06-05
淘宝app是自己开发的还是外包的好
淘宝是一个非常大的购物平台,在智能设备上提供了淘宝app,使得用户可以通过手机进行购物和交易。对于一个企业来说,开发一个移动应用程序是必须的,“自己开发”和“外包”是两种常用的方式。但是,什么方式更好呢?本文将探讨这个问题。“自己开发”是指企业通过自己的内
2023-05-31
恋与制作人自由聊天app
恋与制作人是一款集合了恋爱养成和音乐制作元素的手游,自推出以来广受欢迎。为了满足玩家更多需求,游戏开发方推出了相应的社交APP——恋与制作人自由聊天app。这款APP旨在让玩家更深入地了解游戏中的角色,以及与其他玩家互动交流。其原理是通过与游戏服务器相连接
2023-05-30
©2015-2021 自建app开发平台 www.appbyme.cn 蜀ICP备17005078号-1