可以自己制作的计算器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的原理资料自学app是一种通过互联网技术,将丰富的学习资
2023-06-05
自写app与树莓派制作智能小车
随着科技的不断进步,智能化已经成为了日常生活中的一个趋势,而智能小车也是智能化发展的一个重要方向。在这篇文章中,我们将介绍自写app与树莓派制作智能小车的原理和详细步骤。一、原理我们要制作的智能小车是基于树莓派的,它的运行需要通过树莓派来实现。我们可以通过
2023-06-05
自学python 多久可以开发app
Python是一种动态解释型语言,不但易于学习,而且拥有强大的功能,尤其适用于数据处理和应用程序开发。对于初学者来说,自学Python也不算难事。那么,自学Python需要多久才能够开发出自己的app呢?本文将以原理和详细介绍的形式来解答这个问题。一、Py
2023-06-05
自己开发的app怎么发
自己开发的app需要通过各大应用商店进行发布,应用商店是移动应用的核心分发平台,可以帮助开发者将应用推广给更多用户。这里我们将对自己开发的app如何发布做一个详细的介绍。1、申请开发者账号要在应用商店发布自己的app,必须首先申请开发者账号。苹果公司的应用
2023-06-05
自己制作的app怎么发布到浏览器
发布一个自己制作的APP到浏览器中,主要有两种方式:一种是通过Web app的方式发布,另一种是通过PWA(Progressive Web App)方式发布。下面将详细介绍这两种方式的原理和具体操作方法。一、Web App的发布方式Web App是一种基于
2023-06-05
自动开发小程序app
随着智能手机的普及,移动互联网的需求迅速增长,各种APP也层出不穷。其中,小程序APP因为其轻量级、无需下载安装、与主平台融合度高等特点,越来越受欢迎。开发小程序APP有多种方式,这里介绍一种自动化开发小程序APP的方法。## 原理自动开发小程序APP的方
2023-06-05
制作自己的商铺app
现在手机应用程式已经成为人类生活必不可少的一部分,每个人手机里面都会有几十个不同的应用程式。对于企业来说,自己的应用程式那么重要吗?当一个消费者需要购买一件商品时,想想它们会查找谁,第一个想到的就是使用手机寻找合适的商铺app,因此,制作自己的商铺APP,
2023-06-05
怎样自己制作与疯狂猜歌类似app
如果你是一位有创意并且喜欢游戏的人,想要自己制作与疯狂猜歌类似的APP是非常不错的选择。下面我将介绍一些应用程序的主要功能和技术,以便你可以获得一些有关如何开发一款成功的猜歌类应用程序的基本知识。1.主要功能疯狂猜歌类似app的主要功能是让玩家通过听歌识曲
2023-06-05
可以自建服务器的app
自建服务器是现在互联网领域非常流行的一个趋势,使用自己的服务器可以提高系统的灵活性以及安全性,也可以实现更多的自定义需求。而现在,通过一些开源的软件和应用程序,大家可以非常容易地搭建出自己的服务器。下面,本文将为大家详细介绍如何在手机或电脑上自建服务器的a
2023-05-30
开发属于自己的交易所平台app
随着数字货币的发展,交易所平台的需求也越来越大。如果你也想开发属于自己的交易所平台app,以下是一些原则和详细介绍,帮助你了解需要考虑的事项。原理:一个交易所平台app的原理是将数字货币与法币的兑换交易过程转移到手机或平板电脑上,方便用户在任何时间和地点进
2023-05-30
国外免费自助中文app制作
在国外,有一些免费的自助中文app制作平台可以让用户快速创建自己的app。这些平台主要提供基本的模板和工具箱,让用户根据自己的需要添加自己的内容和功能。下面将简单介绍几个比较知名的自助中文app制作平台。1. Appy PieAppy Pie是一个非常受欢
2023-05-30
app开发自学教程高级技能教程
随着智能手机的普及,App成为人们日常生活中必不可少的应用程序。App开发人才也越来越受到市场的青睐。如果你想成为一名优秀的App开发人员,在自学的过程中,不仅需要学习基础语言和框架,还需要掌握一些高级技能,以便开发出更加复杂、功能强大的App。下面我将为
2023-05-30
©2015-2021 自建app开发平台 www.appbyme.cn 蜀ICP备17005078号-1