可以自己制作的计算器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软件
1. 微信公众号微信公众号是做自媒体必不可少的一款应用。通过微信公众号,用户可以发布文章、图文、音频等多种形式的内容,吸引读者关注、转发和评论。同时,公众号也提供了统计数据功能,可以分析公众号的阅读量、转发量等数据,为后续的运营提供参考。此外,微信公众号还
2023-06-05
自建app网站
自建app网站是一种将应用程序(APP)运用于网页的技术。它可以将原本只能在移动设备上使用的应用程序适配至网页,使得用户可以通过电脑或者手机浏览器来使用移动端应用程序,从而使得更多用户可以方便地获得该应用程序的服务。那么,我们来具体介绍一下自建app网站的
2023-06-05
自己做电商app需要多少钱
做电商APP的成本与开销是与项目的规模和复杂度紧密相关的。一般情况下,建立应用的成本分为三个部分:1.开发成本:如果你技术娴熟,自己能够制作出自己的电商应用,或者你有个技术团队,在这种情况下,你需要支付针对全部工程代码开发的费用。如果你不想招聘开发者,也可
2023-06-05
自己做一个app卖酒合法吗
自己做一个卖酒的APP是可行的,但需要遵循一定的法律法规以及行业配套政策。首先,需要注意的是,酒类是一种需要审批的商品。在我国,酒类的生产、流通、销售等方面均需要经过相关主管部门的审批和认证。对于卖酒的APP而言,其所售卖的酒类产品是否已经获得审批和认证是
2023-06-05
自己开发一个app可以吗安全吗
自己开发一个App非常有可能会涉及到一些安全问题和潜在的风险。一个优秀的App需要经过严谨的开发和测试,以确保它的稳定性和安全性。下面,我们将详细介绍自己开发一个App可能面临的安全问题及其原理。首先,App的安全问题主要包括以下几个方面。1. 数据安全当
2023-06-05
怎么把网站自己做成app
将网站做成APP是一个非常流行的操作,它能够为用户提供更好的使用体验,同时也能够为网站持续吸引新用户。本文将介绍如何将网站自己做成APP。一、技术方案要将网站做成APP,需要使用技术方案实现。常见的实现方式有两种:一种是基于跨平台开发框架技术,比如Reac
2023-06-05
有什么app可以自己做歌曲
在当今数字化的时代,越来越多的人开始尝试自己创作音乐,而随之而来的是各种创作工具的出现。在这篇文章中,我们将介绍几个可用于创建自己的音乐的应用程序。这些应用程序既可以在iOS,也可以在Android上使用。在介绍这些应用之前,我们先来了解一下音乐创作的原理
2023-06-05
有想法开发app自己不会弄怎么办
对于非开发者而言,开发一个专业的App可能是一项非常艰巨的任务。不过,如果你有一些创意并想把它实现成App的形式,那么你可以尝试以下方法:1. 找到一些App开发工具。如今,市场上有很多APP 开发工具供您选择,无需和程序员们一起工作,你就可以在短时间内为
2023-06-05
用别人的硬件开发自己的app
当我们想要开发一个移动应用程序时,通常需要一些硬件设备来帮助我们构建和测试我们的应用程序。这些硬件设备可以包括电脑、智能手机、平板电脑、传感器、蓝牙设备等。此外,我们还可以利用一些其他的硬件设备来扩展我们的应用程序的功能。下面将详细介绍如何使用别人的硬件设
2023-06-05
死神自己编程开发app
要开发一个自己的app,通常需要学习一门或多门编程语言、了解移动开发领域的知识,还需要选择开发工具和技术方案。在这篇文章中,我将介绍一下我个人编程开发app的原理和方法。首先,我选择学习Android开发相关的知识和技术。Android开发需要掌握Java
2023-05-31
如何开发自己的app平台
开发一款自己的app平台,需要我们关注多个方面的问题,包括设计、技术选择、功能实现、用户体验等等。下面,我将从几个方面来介绍如何开发一款自己的app平台。**一、概述**开发移动应用程序的过程对于开发者来说可能是艰难而烦人的,但是一旦应用程序设计好了,将它
2023-05-30
可以自己做的漫画的app
做漫画的App有很多,其中比较流行的有Clip Studio Paint、Procreate、MediBang Paint等等。这些App都是专供建立卡通和漫画或其他任意创作领域的艺术家使用的。下面,本文将对如何在自己的App里制作漫画进行详细介绍:1.
2023-05-30
©2015-2021 自建app开发平台 www.appbyme.cn 蜀ICP备17005078号-1