可以自己制作的计算器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可以让用户在手机上随时随地进行绘画和涂鸦。本文将介绍原理和详细步骤。1. 原理要制作一个笔刷App,需要了解以下基本原理:- Touch事件:在平板电脑和手机上,笔刷跟随用户手指的动作进行绘画。要实现这个功能,需要掌握Touch事件
2023-06-05
自学苹果开发app需要什么基础
苹果开发app不是一件简单的事情,需要掌握一定的编程知识和技术,有一定的计算机基础可以让你更好地理解和实践,下面我来为大家介绍学习苹果开发app需要的基础知识。1.编程语言在苹果开发app中,需要掌握好编程语言Objective-C或者Swift。其中Ob
2023-06-05
自己可以做个app需要哪些技术
如果你想自己做一个app,需要掌握以下技术:1.编程语言最常用的编程语言有Java、Swift、Objective-C、Kotlin和JavaScript等。Java常用于Android端开发;Swift和Objective-C则主要用于iOS端的开发;K
2023-06-05
自己制作的app没功能
如果你自己制作的app没有功能,那么可能有以下几个原因:1. 缺乏设计思路设计一个app并不简单,它需要你有很强的逻辑思维、交互设计能力和审美观。如果你的app缺乏想法,或者你没有考虑你的用户需要什么,那么你的app就会缺乏功能,甚至无法实现你的设计构想。
2023-06-05
有自己做路书的app么
做路书的 App,指的是通过一定的工具和技巧,将自己的旅游路线、旅游过程记录下来,并以图文的形式呈现出来,供其他游客借鉴和分享。这种 app 的原理,主要是建立在位置记录、轨迹追踪、照片拍摄、文字说明和社交分享等多个方面的基础上。首先,位置记录是道路书 a
2023-06-05
一个app开发者的自白
作为一个app开发者,我深深地了解到了这个行业的缤纷与挑战。下面我将与你分享我的经验和想法。首先,我想强调app的开发需要花费大量的时间和精力。在开始之前,开发者需要进行大量的需求分析和调研。这将帮助你确定你的app需要提供哪些功能和特性,对想要使用它的用
2023-06-05
捏咔app如何自己做模板
捏咔是一款非常流行的二次元人物生成工具,其可以通过使用预设的模板或自定义模板创建虚拟形象。自己做模板需要掌握一定的基础知识和技能,下面将详细介绍此过程的原理和步骤。一、捏咔模板原理在捏咔当中,每个二次元人物虚拟形象都由各种不同的参数组成。这些参数包括头部、
2023-05-30
大学生就能自己做手机app
在当今互联网发展日新月异的时代,手机应用市场日益火热,手机APP成为了人们日常生活中重要的一部分,也成为了一项新的创业创新方式。大学生可以利用自身的优势和各类资源,开发自己的手机APP,成为创新创业的一员。下面为大家详细介绍如何自己开发手机APP。1. 学
2023-05-30
不懂技术也能自己开发app
在当今科技高速发展的年代,App开发不再是一项仅限于专业人员的事情。普通人们也可以通过一些简单的工具、软件或资源来设计、开发、测试、发布自己的应用程序。在本文中,我们将介绍如何让不懂技术的人也能够开发自己的App。一、选择合适的App开发工具选择合适的Ap
2023-05-30
app自主开发和外包的区别
App自主开发和外包开发,它们之间存在一些差别,下面将对这两种开发方式的原理进行较为详细的介绍。首先,App自主开发是指企业或个人自行组建研发团队,进行应用的设计、开发、测试和发布等全过程。在自主开发过程中,企业或个人通常会自己付出研发的成本,也就是说,由
2023-05-30
app开发到底应该自己选
在现如今的互联网时代,移动设备的普及,让很多企业看到了移动应用开发的商机,这也使得APP开发越来越受到企业和开发者的关注。但是面对众多的APP开发工具,开发者该如何选择呢?以下为您解析APP开发工具选择的原理和详细介绍。1.自我技能水平与所需工具匹配度通过
2023-05-30
app程序开发自学
随着移动互联网的快速发展和普及,移动应用程序已经成为人们日常生活中不可或缺的一部分,同时也是市场竞争的核心。因此,app程序开发的需求也越来越迫切。作为一位有志于学习app开发的初学者,需要了解app程序开发的原理和详细步骤,掌握相关技能和工具,才能进一步
2023-05-30
©2015-2021 自建app开发平台 www.appbyme.cn 蜀ICP备17005078号-1